2017-12-01 19 views
0

深い学習のチュートリアルのウェブサイトで、私はこのコードが以下のように画像を読むことが分かった。python:深い学習のためにRGB画像を読む

cv2.resize(cv2.imread(folder + name, 1), (100, 200)).reshape(3, 100, 200) 

このコードは、画像の形状を(100,200,3)から(3,100,200)に変更します。 関数が行列の形をどのように変えているかを調べようとしましたが、奇妙な出力がありました。
2x4 RGB画像(d)を次のようにします。

d = array([[[ 1, 2, 3],[ 4, 5, 6],[ 7, 8, 9], [10, 11, 12]],[[13, 14, 15],[16, 17, 18], [19, 20, 21],[22, 23, 24]]]) 
d.shape: (2, 4, 3) 

作り直す適用した後、それは

d.reshape(3,2,4) 
array([[[ 1, 2, 3, 4], 
     [ 5, 6, 7, 8]], 
     [[ 9, 10, 11, 12], 
     [ 13, 14, 15, 16]], 
     [[ 17, 18, 19, 20], 
     [ 21, 22, 23, 24]]]) 

を示ししかし、我々は以下のようにイメージを表現したいので、私は、これは適切な表現ではないと思います。

enter image description here したがって、以下のように画像を変換する必要があります。

d.reshape(3,2,4) 
array([[[ 1, 4, 7, 10], 
     [ 13, 16, 19, 22]],#R layer 
     [[ 2, 5, 8, 11], 
     [ 14, 17, 20, 23]],#G layer 
     [[ 3, 6, 9, 12], 
     [ 15, 18, 21, 24]]])#B layer 

私の理解は間違っていますか?知識があれば私を助けてください。

以下のコード全体を入力します。

from torch import nn 
    from torch.nn import functional as F 
    from torch.autograd import Variable 
    from sklearn.model_selection import train_test_split 
    import numpy as np 
    from collections import Counter 
    import os 
    import cv2 
    import torch.optim as optim 
    import torch.utils.data 


    def read_labels(file): 
     dic = {} 
     with open(file) as f: 
     reader = f 
     for row in reader: 
      dic[row.split(",")[0]] = row.split(",")[1].rstrip() #rstrip(): eliminate "\n" 
     return dic 

    image_names= os.listdir("../train") 
    label_dic = read_labels("../labels.csv") 

    labels = [] 
    images =[] 

    for name in image_names: 
     images.append(cv2.resize(cv2.imread("../train/"+name,1), (100, 200)).reshape(3,100,200)) 
     labels.append(label_dic[os.path.splitext(name)[0]]) 

    images = np.asarray(images) 


    """ 
    Assign numbers for each labels 
    """ 

    tmp_labels = labels 
    uniq_labels = set(tmp_labels) # eliminate duplication 
    num_breeds = len(Counter(labels)) # number of breeds 
    uniqu_labels_index = dict((label, i) for i, label in enumerate(uniq_labels)) #create dictionary and assign number for each labels 

    labels_num = [uniqu_labels_index[label] for i,label in enumerate(labels)] 
    labels_num = np.array(labels_num) 

    """ 
    Data distribution 
    """ 
    N = len(images) 
    N_train = int(N * 0.7) 
    N_test = int(N*0.2) 

    X_train, X_tmp, Y_train, Y_tmp = train_test_split(images, labels_num, train_size=N_train) 
    X_validation, X_test, Y_validation, Y_test = train_test_split(X_tmp, Y_tmp, test_size=N_test) 

    """ 
    Model Definition 
    """ 


    # CNN Model (2 conv layer) 
    class CNN(nn.Module): 
     def __init__(self): 
      super(CNN, self).__init__() 
      self.layer1 = nn.Sequential(
       nn.Conv2d(3,34, kernel_size=5,padding= 2), 
       nn.Dropout2d(), 
       nn.BatchNorm2d(34), 
       nn.ReLU(), 
       nn.MaxPool2d(2)) 
      self.layer2 = nn.Sequential(
       nn.Conv2d(34, 68, kernel_size=5,padding= 2), 
       nn.BatchNorm2d(68), 
       nn.ReLU(), 
       nn.MaxPool2d(2)) 
      self.fc1 = nn.Linear(1700,300) 
      self.fc2 = nn.Linear(300,num_breeds) 

     def forward(self, x): 
      out = self.layer1(x) 
      #print out.data.shape 
      out = self.layer2(out) 
      #print out.data.shape 
      out = out.view(out.size(0), -1) 
      #print out.data.shape 
      out =self.fc1(out) 
      #out = F.dropout(out) 
      #out = self.fc2(out) 
      return F.log_softmax(out) 

     def accuracy(self,outputs,labels): 
      #for i, (images_val, labels_val) in enumerate(val_loader): 

       # print images.shape 
      # images_val = Variable(images_val).float() 
       # labels_val = Variable(labels_val).float().type(torch.LongTensor) 
       # outputs_val = CNN(images_val) 

      inference = np.argmax(outputs.data.numpy(),axis=1) 
      answers = labels.data.numpy() 
      correction = np.equal(inference,answers) 
      return np.sum(correction)/float(len(correction)) 

    CNN = CNN() 

    """ 
    Training 
    """ 
    batch_size = 100 
    learning_rate =0.01 
    # Data Loader (Input Pipeline) 
    train = torch.utils.data.TensorDataset(torch.from_numpy(X_train), torch.from_numpy(Y_train)) 
    train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size, shuffle=True) 

    val = torch.utils.data.TensorDataset(torch.from_numpy(X_validation), torch.from_numpy(Y_validation)) 
    val_loader = torch.utils.data.DataLoader(val, batch_size=len(X_validation), shuffle=True) 

    test = torch.utils.data.TensorDataset(torch.from_numpy(X_test), torch.from_numpy(Y_test)) 
    test_loader = torch.utils.data.DataLoader(test, batch_size=len(X_test), shuffle=True) 


    criterion = nn.CrossEntropyLoss() 
    optimizer = torch.optim.Adam(CNN.parameters(), lr=learning_rate) 

    for epoch in range(250): # loop over the dataset multiple times 
     running_loss = 0.0 
     for i, (images, labels) in enumerate(train_loader): 

      images = Variable(images).float() 
      labels = Variable(labels).float().type(torch.LongTensor) 
      # Forward + Backward + Optimize 
      optimizer.zero_grad() 
      outputs = CNN(images) 
      loss = criterion(outputs, labels) 
      loss.backward() 
      optimizer.step() 

      running_loss += loss.data[0] 


     accuracy = CNN.accuracy(outputs,labels) 
     print 
     print "epoch :",epoch 
     print 'loss:' ,float(running_loss)/2000 
     print "accuracy :",accuracy 
     running_loss = 0.0 

    print('Finished Training') 
    for i, (images, labels) in enumerate(test_loader): 

      images = Variable(images).float() 
      labels = Variable(labels).float().type(torch.LongTensor) 
      optimizer.zero_grad() 
      outputs = CNN(images) 
    inference = np.argmax(outputs.data.numpy(),axis=1) 
    answers = labels.data.numpy() 
    correction = np.equal(inference,answers) 
    print np.sum(correction)/float(len(correction)) 
+1

をやっているかを見ることができるのだろうか?変換された形状のRGBデータはどこにプッシュされていますか?あなたが従うべきチュートリアルへのリンクがありますか?データの形状は、どのような「深い学習」アルゴリズムが使用されているかによって異なります。 – jmunsch

答えて

0

Numpyの機能が使用されます。

shapeの方法では、各レイヤーにどのくらい多くの要素が配列されているかが示されます。 だから、あなたの例では:

d = array([ 
      [[ 1, 2, 3],[ 4, 5, 6],[ 7, 8, 9],[10, 11, 12]], #1st layer 1st element (4 lists inside with 3 numbers each) 
      [[13, 14, 15],[16, 17, 18], [19, 20, 21],[22, 23, 24]] #1st layer 2nd element (4 lists inside with 3 numbers each) 
     ]) 

第一層は二つのリストは、第2層4つのリストを持っており、第三には、3つの数字を持っています。

reshape(3,2,4)に電話すると、最初のレイヤーに3つのリストが、2つ目のレイヤーに2つのリストが、3つ目のレイヤーに4つの数字が表示されます。

要素の順序は変わりません。形状を変更するだけです。あなたの例では、imshowで修正された画像を見ると、reshapeコマンドが画像を乱していることがわかります。

試してみてください。そして、

image = cv2.imread(folder + name, 1) 
cv2.imshow('image',image) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

reshapedimage =cv2.resize(cv2.imread(folder + name, 1), (100, 200)) 
cv2.imshow('image',reshapedimage) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

あなたは、各コマンドがあなたのイメージに出力形状が使用されている

関連する問題