def run(subBatchSize=5, index=0, loadWeightFile=None): loadweight = True my = myCNN() # Read dataset base = 'C:/Users/Abdulkadir/Desktop/Datasets/Pet Dataset/10class' dir = base + '/' filename = ('subset1.pkl', ) (images, labels) = load_pet_dataset(dir, filename) x = T.matrix('x') # data input symbolic variable y = T.ivector('y') # labels symbolic variable # -----< Construction of Network Model >----- layer0 = x.reshape((subBatchSize, 64, 64, 3)).transpose(0, 3, 1, 2) [layer1, layer1_w, layer1_b] = my.convolutionLayer(featureMaps=layer0, featureMapShape=(subBatchSize, 3, 64, 64), kernelShape=(16, 3, 15, 15), bias=0.1) layer2 = my.maxPoolingLayer(featureMaps=layer1, poolingShape=(2, 2), stride=2) layer3 = my.reLuLayer(featureMaps=layer2) [layer4, layer4_w, layer4_b ] = my.convolutionLayer(featureMaps=layer3, featureMapShape=(subBatchSize, 32, 25, 25), kernelShape=(32, 16, 11, 11)) layer5 = my.maxPoolingLayer(featureMaps=layer4, poolingShape=(3, 3), stride=3) layer6 = my.reLuLayer(featureMaps=layer5) layer7 = layer6.flatten(2) [layer8, layer8_w, layer8_b] = my.fullyConnectedLayer(inputUnits=layer7, inputDim=32 * 5 * 5, outputDim=64) layer8 = layer8.reshape((subBatchSize, 64)) [error, numOfWrongClass, layer9_w, layer9_b] = my.softmaxLayer(inputVect=layer8, labels=y, inputDim=64, numOfClasses=10) # --------------------< Construction of Training Function >-------------------- if loadweight is True and loadWeightFile is not None: with open(loadWeightFile, 'rb') as w: dataset = pickle.load(w) (param1, param2, param3, param4, param5, param6, param7, param8) = dataset layer1_w.set_value(param1) layer1_b.set_value(param2) layer4_w.set_value(param3) layer4_b.set_value(param4) layer8_w.set_value(param5) layer8_b.set_value(param6) layer9_w.set_value(param7) layer9_b.set_value(param8) loadweight = False print "Pretrained weights were loaded!" # Define symbolic index variable index = T.iscalar('index') # Definition of symbolic training function visualize = function( [index], error, givens={ x: images[index * subBatchSize:(index + 1) * subBatchSize], y: labels[index * subBatchSize:(index + 1) * subBatchSize] }) err = visualize(0) return (layer0.eval({x: images.get_value()[0:subBatchSize]}), layer1.eval({x: images.get_value()[0:subBatchSize]}), layer2.eval({x: images.get_value()[0:subBatchSize]}), layer3.eval({x: images.get_value()[0:subBatchSize]}))
def run(subBatchSize=500, maxEpochNum=100, eta=0.1, trainErrPeriod=5, testErrPeriod=10, logfile='./log.txt', saveWeightFile=None, saveWeightsFor='train', loadWeightFile=None): my = myCNN() # Read dataset base = './datasets/2class' trainSet_dir = base + '/' train_filename = ('subset1.pkl', 'subset2.pkl', 'subset3.pkl', 'subset4.pkl', 'subset5.pkl', 'subset6.pkl', 'subset7.pkl', 'subset8.pkl') (trainImages, trainLabels) = load_pet_dataset(trainSet_dir, train_filename) testSet_dir = base + '/' test_filename = ('subset9.pkl', 'subset10.pkl') (testImages, testLabels) = load_pet_dataset(testSet_dir, test_filename) # Get the number of images in the training set numOfTrainImages = trainImages.get_value().shape[0] # Get the number of images in the test set numOfTestImages = testImages.get_value().shape[0] # Get the sub batch size for training set assert ( numOfTrainImages % subBatchSize == 0 ), "The subbatch size must be a divisor of the number of train images" numOfTrainSubBatches = numOfTrainImages / subBatchSize # Get the sub batch size for test set assert ( numOfTestImages % subBatchSize == 0 ), "The subbatch size must be a divisor of the number of test images" numOfTestSubBatches = numOfTestImages / subBatchSize x = T.matrix('x') # data input symbolic variable y = T.ivector('y') # labels symbolic variable # -----< Construction of Network Model >----- layer0 = x.reshape((subBatchSize, 64, 64, 3)).transpose(0, 3, 1, 2) [layer1, layer1_w, layer1_b] = my.convolutionLayer(featureMaps=layer0, featureMapShape=(subBatchSize, 3, 64, 64), kernelShape=(16, 3, 11, 11), bias=0.1) layer2 = my.maxPoolingLayer(featureMaps=layer1, poolingShape=(3, 3), stride=3) layer3 = my.reLuLayer(featureMaps=layer2) [layer4, layer4_w, layer4_b ] = my.convolutionLayer(featureMaps=layer3, featureMapShape=(subBatchSize, 32, 18, 18), kernelShape=(32, 16, 7, 7)) layer5 = my.maxPoolingLayer(featureMaps=layer4, poolingShape=(2, 2), stride=2) layer6 = my.reLuLayer(featureMaps=layer5) layer7 = layer6.flatten(2) [layer8, layer8_w, layer8_b] = my.fullyConnectedLayer(inputUnits=layer7, inputDim=32 * 6 * 6, outputDim=64) layer8 = layer8.reshape((subBatchSize, 64)) [error, numOfWrongClass, layer9_w, layer9_b] = my.softmaxLayer(inputVect=layer8, labels=y, inputDim=64, numOfClasses=2) # --------------------< Construction of Training Function >-------------------- # Load weight if it is desired loadweight = True if loadweight is True and loadWeightFile is not None: with open(loadWeightFile, 'rb') as w: weights = pickle.load(w) (param1, param2, param3, param4, param5, param6, param7, param8) = weights layer1_w.set_value(param1) layer1_b.set_value(param2) layer4_w.set_value(param3) layer4_b.set_value(param4) layer8_w.set_value(param5) layer8_b.set_value(param6) layer9_w.set_value(param7) layer9_b.set_value(param8) loadweight = False print "Pretrained weights were loaded!" # Define symbolic index variable index = T.iscalar('index') # Define parameters params = [ layer1_w, layer1_b, layer4_w, layer4_b, layer8_w, layer8_b, layer9_w, layer9_b ] # Take the derivative of error function with respect to parameters grads = T.grad(cost=error, wrt=params) # Define updates updates = [(w, w - eta * delta) for w, delta in zip(params, grads)] # Definition of symbolic training function training = function( [index], error, givens={ x: trainImages[index * subBatchSize:(index + 1) * subBatchSize], y: trainLabels[index * subBatchSize:(index + 1) * subBatchSize] }, updates=updates, ) # Definiton of the symbolic function computing the training error computeTrainingError = function( [index], numOfWrongClass, givens={ x: trainImages[index * subBatchSize:(index + 1) * subBatchSize], y: trainLabels[index * subBatchSize:(index + 1) * subBatchSize] }) # Definiton of the symbolic testing function testing = function( [index], numOfWrongClass, givens={ x: testImages[index * subBatchSize:(index + 1) * subBatchSize], y: testLabels[index * subBatchSize:(index + 1) * subBatchSize] }) print "The total number of training images in the dataset : " + str( numOfTrainImages) print "The total number of test images in the dataset : " + str( numOfTestImages) # Log file with open(logfile, "a") as logf: logf.write('The total number of training images in the dataset : ' + str(numOfTrainImages) + '\n') logf.write('The total number of test images in the dataset : ' + str(numOfTestImages) + '\n') minErr = numOfTrainImages + numOfTestImages for epoch in range(1, maxEpochNum + 1): for subBatchIndex in range(numOfTrainSubBatches): err = training(subBatchIndex) if (epoch % trainErrPeriod == 0) or (epoch == 1): # Compute the training error trainingError = [ computeTrainingError(inx) for inx in range(numOfTrainSubBatches) ] # Get the total wrong classified number of elements in the training set totalWrongClass = np.sum(trainingError) print "Epoch : " + str(epoch) + " Training error : %" + str( totalWrongClass * 100.0 / numOfTrainImages) + " " + str(totalWrongClass) # Write log file with open(logfile, "a") as logf: logf.write('Epoch : ' + str(epoch) + '\n') logf.write('Training : ' + str(totalWrongClass * 100.0 / numOfTrainImages) + ' ' + str(totalWrongClass) + '\n') if (epoch % testErrPeriod == 0) or (epoch == 1): # Compute the testing error testingError = [testing(inx) for inx in range(numOfTestSubBatches)] # Get the total wrong classified number of elements in the test set totalTestWrongClass = np.sum(testingError) print "\t\t Testing error : %" + str( totalTestWrongClass * 100.0 / numOfTestImages) + " " + str(totalTestWrongClass) # Write log file with open(logfile, "a") as logf: logf.write('Testing : ' + str(totalTestWrongClass * 100.0 / numOfTestImages) + ' ' + str(totalTestWrongClass) + '\n') # Save weights if saveWeightsFor == 'train': currentErr = totalWrongClass elif saveWeightsFor == 'test': currentErr = totalTestWrongClass else: print "Please enter the option name to save weights for training or test!" if minErr > currentErr and saveWeightFile is not None: print "Weights are saved!" minErr = currentErr with open(saveWeightFile, 'wb') as w: pickle.dump((layer1_w.get_value(), layer1_b.get_value(), layer4_w.get_value(), layer4_b.get_value(), layer8_w.get_value(), layer8_b.get_value(), layer9_w.get_value(), layer9_b.get_value()), w, protocol=pickle.HIGHEST_PROTOCOL)
def run(subBatchSize=5, index=0, loadWeightFile=None): loadweight = True my = myCNN() # Read dataset base = 'C:/Users/Abdulkadir/Desktop/Datasets/cifar-10-python/cifar-10-batches-py' dir = base + '/' filename = ('data_batch_1',) (images, labels) = load_cifar_dataset(dir, filename) x = T.matrix('x') # data input symbolic variable y = T.ivector('y') # labels symbolic variable # -----< Construction of Network Model >----- layer0 = x.reshape((subBatchSize, 3, 32, 32)) [layer1, layer1_w, layer1_b] = my.convolutionLayer(featureMaps=layer0, featureMapShape=(subBatchSize, 3, 32, 32), kernelShape=(32, 3, 5, 5), bias=0.1 ) layer2 = my.maxPoolingLayer(featureMaps=layer1, poolingShape=(2, 2), stride=2 ) layer3 = my.reLuLayer(featureMaps=layer2) [layer4, layer4_w, layer4_b] = my.convolutionLayer(featureMaps=layer3, featureMapShape=(subBatchSize, 32, 14, 14), kernelShape=(50, 32, 5, 5) ) layer5 = my.maxPoolingLayer(featureMaps=layer4, poolingShape=(2, 2), stride=2 ) layer6 = my.reLuLayer(featureMaps=layer5) [layer7, layer7_w, layer7_b] = my.convolutionLayer(featureMaps=layer6, featureMapShape=(subBatchSize, 50, 5, 5), kernelShape=(64, 50, 5, 5) ) layer8 = my.maxPoolingLayer(featureMaps=layer7, poolingShape=(1, 1), stride=1 ) layer9 = my.reLuLayer(featureMaps=layer8) layer10 = layer9.flatten(2) layer13 = layer10.reshape((subBatchSize, 1*1*64)) [error, numOfWrongClass, layer14_w, layer14_b] = my.softmaxLayer(inputVect=layer13, labels=y, inputDim=1*1*64, numOfClasses=10 ) # --------------------< Construction of Training Function >-------------------- if loadweight is True and loadWeightFile is not None: with open(loadWeightFile, 'rb') as w: dataset = pickle.load(w) (param1, param2, param3, param4, param5, param6, param7) = dataset layer1_w.set_value(param1) layer1_b.set_value(param2) layer4_w.set_value(param3) layer4_b.set_value(param4) layer7_w.set_value(param5) layer7_b.set_value(param6) print "Epoch : " + str(param7) loadweight = False print "Pretrained weights were loaded!" # Define symbolic index variable index = T.iscalar('index') # Definition of symbolic training function visualize = function([index],error, givens={ x: images[index * subBatchSize: (index + 1) * subBatchSize], y: labels[index * subBatchSize: (index + 1) * subBatchSize] } ) err = visualize(0) return (layer0.eval({x: images.get_value()[0:subBatchSize]}), layer1.eval({x: images.get_value()[0:subBatchSize]}))
def run(): # オプション情報の設定・表示 epochs = max(1, args.epochs) # 総エポック数(繰り返し回数) batchsize = max(1, args.batchsize) # バッチサイズ model_filepath = args.model # 学習結果のモデルの保存先ファイル # print('device: {0}'.format(dev_str), file=sys.stderr) # print('epochs: {0}'.format(epochs), file=sys.stderr) # print('batchsize: {0}'.format(batchsize), file=sys.stderr) # print('model file: {0}'.format(model_filepath), file=sys.stderr) # print('', file=sys.stderr) # 画像の縦幅・横幅・チャンネル数の設定 width = 240 # MNIST文字画像の場合,横幅は 28 pixels height = 240 # MNIST文字画像の場合,縦幅も 28 pixels channels = 3 # MNIST文字画像はグレースケール画像なので,チャンネル数は 1 color_mode = 0 if channels == 1 else 1 # 学習データの読み込み labels, imgfiles, labeldict, labelnames = read_image_list( IMAGE_LIST, data_dir=DATA_DIR) n_samples = len(imgfiles) # 学習データの総数 n_classes = len(labeldict) # クラス数 with open(MODEL_DIR + 'labeldict.pickle', 'wb') as f: pickle.dump(labeldict, f) # ラベル名からラベル番号を与える辞書をファイルに保存 with open(MODEL_DIR + 'labelnames.pickle', 'wb') as f: pickle.dump(labelnames, f) # ラベル番号からラベル名を与える配列をファイルに保存 # 評価用データの読み込み labels_ev, imgfiles_ev, labeldict, dmy = read_image_list( IMAGE_LIST_EV, data_dir=DATA_DIR, dic=labeldict) n_samples_ev = len(imgfiles_ev) # 評価用データの総数 # 画像認識器の作成 model = myCNN(width, height, channels, n_classes) model = model.to(dev) # 損失関数の定義 loss_func = nn.CrossEntropyLoss() # softmax cross entropy # オプティマイザーの用意 optimizer = optim.Adam(model.parameters()) # 最もaccuracyが高かったepochとそのaccuracy best_epoch = 0 best_accuracy = 0.0 # 学習処理ループ for e in range(epochs): # 現在のエポック番号を表示 # print('Epoch {0}'.format(e + 1), file=sys.stderr) # 損失関数の値が小さくなるように識別器のパラメータを更新 model.train() n_input = 0 sum_loss = 0 perm = np.random.permutation(n_samples) batch_range = min(batchsize, n_samples % batchsize) for i in range(0, n_samples, batchsize * 10): # 高速化のため,ミニバッチ10個につき1個しか学習に用いないことにする model.zero_grad() x = torch.tensor(load_images( imgfiles, ids=perm[i: i + batch_range], mode=color_mode), device=dev) # 学習データを読み込む t = torch.tensor(labels[perm[i: i + batch_range]], device=dev, dtype=torch.long) loss = loss_func(model(x), t) loss.backward() optimizer.step() sum_loss += float(loss) * len(x) n_input += len(x) del loss del t del x # 損失関数の現在値を表示 # print(' train loss = {0:.4f}'.format(sum_loss / n_input), file=sys.stderr) # 評価用データに対する識別精度を計算・表示 model.eval() n_failed = 0 for i in range(0, n_samples_ev, batchsize): x = torch.tensor(load_images(imgfiles_ev, ids=np.arange(i, min( n_samples_ev, i + batchsize)), mode=color_mode), device=dev) # 評価用データを読み込む t = torch.tensor(labels_ev[i: i + batchsize], device=dev, dtype=torch.long) y = model.classify(x) y_cpu = y.to('cpu').detach().numpy().copy() t_cpu = t.to('cpu').detach().numpy().copy() n_failed += np.count_nonzero(np.argmax(y_cpu, axis=1) - t_cpu) del y_cpu del t_cpu del y del x del t acc = (n_samples_ev - n_failed) / n_samples_ev if best_accuracy < acc: best_epoch = e best_accuracy = acc # print(' accuracy = {0:.2f}%'.format(100 * acc), file=sys.stderr) # 現在のモデルをファイルに自動保存 # torch.save(model.to('cpu').state_dict(), MODEL_DIR + # 'model_ep{0}.pth'.format(e + 1)) model = model.to(dev) # print('', file=sys.stderr) # 最終結果のモデルをファイルに保存 # torch.save(model.to('cpu').state_dict(), model_filepath) return (best_epoch, best_accuracy)
# 画像の縦幅・横幅・チャンネル数の設定 width = 240 # MNIST文字画像の場合,横幅は 28 pixels height = 240 # MNIST文字画像の場合,縦幅も 28 pixels channels = 3 # MNIST文字画像はグレースケール画像なので,チャンネル数は 1 color_mode = 0 if channels == 1 else 1 # ラベル名とラベル番号を対応付ける辞書をロード with open(MODEL_DIR + 'labeldict.pickle', 'rb') as f: labeldict = pickle.load(f) with open(MODEL_DIR + 'labelnames.pickle', 'rb') as f: labelnames = pickle.load(f) n_classes = len(labelnames) # 学習済みの画像認識器をロード model = myCNN(width, height, channels, n_classes) model.load_state_dict(torch.load(model_filepath)) model = model.to(dev) model.eval() # 入力画像に対し認識処理を実行 if in_filepath == '': ### ファイル名を指定せずに実行した場合・・・全評価用データに対する識別精度を表示 ### labels_ev, imgfiles_ev, labeldict, dmy = read_image_list(IMAGE_LIST_EV, DATA_DIR, dic=labeldict) # 評価用データの読み込み n_samples_ev = len(imgfiles_ev) # 評価用データの総数 n_failed = 0 # row: expected, col: actual eval_matrix = [[0 for j in range(7)] for i in range(7)] for i in range(0, n_samples_ev, batchsize):
def model(imageId, weightFile=None): my = myCNN() myd = myDECNN() # Read dataset trainSet_dir = 'C:\\Users\\Abdulkadir\\Desktop\\Datasets\\cifar-10-python\\cifar-10-batches-py\\' train_filename = ('data_batch_1',) (trainImages, trainLabels) = load_cifar_dataset(trainSet_dir, train_filename) (images, labels) = (trainImages, trainLabels) assert (weightFile is not None), "Please enter a valid weight file!" x = T.matrix('x') # data input symbolic variable y = T.ivector('y') # labels symbolic variable # -----< Construction of Network Model >----- subBatchSize = 1 layer0 = x.reshape((subBatchSize, 3, 32, 32)) [layer1, layer1_w, layer1_b] = my.convolutionLayer(featureMaps=layer0, featureMapShape=(subBatchSize, 3, 32, 32), kernelShape=(32, 3, 5, 5), bias=0.1 ) layer2, maxlocsLayer2 = myd.maxPoolingLayer(featureMaps=layer1, featureMapShape=(subBatchSize, 32, 28, 28), poolingSize=2) layer3 = my.reLuLayer(featureMaps=layer2) [layer4, layer4_w, layer4_b] = my.convolutionLayer(featureMaps=layer3, featureMapShape=(subBatchSize, 32, 14, 14), kernelShape=(50, 32, 5, 5) ) layer5, maxlocsLayer4 = myd.maxPoolingLayer(featureMaps=layer4, featureMapShape=(subBatchSize, 50, 10, 10), poolingSize=2) layer6 = my.reLuLayer(featureMaps=layer5) [layer7, layer7_w, layer7_b] = my.convolutionLayer(featureMaps=layer6, featureMapShape=(subBatchSize, 50, 5, 5), kernelShape=(64, 50, 5, 5)) layer8, maxlocsLayer8 = myd.maxPoolingLayer(featureMaps=layer7, featureMapShape=(subBatchSize, 64, 1, 1), poolingSize=1) layer9 = my.reLuLayer(featureMaps=layer8) layer10 = layer9.flatten(2) layer13 = layer10.reshape((subBatchSize, 1 * 1 * 64)) [error, numOfWrongClass, layer14_w, layer14_b] = my.softmaxLayer(inputVect=layer13, labels=y, inputDim=1 * 1 * 64, numOfClasses=10 ) T.set_subtensor(layer9[0,:1,:,:], 0) T.set_subtensor(layer9[0,1:,:,:], 0) delayer8 = myd.reLuLayer(featureMaps=layer9) delayer7 = myd.maxUnpoolingLayer(input=delayer8, featureMapShape=(subBatchSize, 64, 1, 1), maxLocations=maxlocsLayer8) delayer6, delayer6_w = myd.deconvolutionLayer(featureMaps=delayer7, featureMapShape=(subBatchSize, 64, 1, 1), kernelShape=(50, 64, 5, 5), w=layer7_w) delayer5 = myd.reLuLayer(featureMaps=delayer6) delayer4 = myd.maxUnpoolingLayer(input=delayer5, featureMapShape=(subBatchSize, 50, 10, 10), maxLocations=maxlocsLayer4) delayer3, delayer3_w = myd.deconvolutionLayer(featureMaps=delayer4, featureMapShape=(subBatchSize, 50, 10, 10), kernelShape=(32, 50, 5, 5), w=layer4_w) delayer2 = myd.reLuLayer(featureMaps=delayer3) delayer1 = myd.maxUnpoolingLayer(input=delayer2, featureMapShape=(subBatchSize, 32, 28, 28), maxLocations=maxlocsLayer2) delayer0, delayer0_w = myd.deconvolutionLayer(featureMaps=delayer1, featureMapShape=(subBatchSize, 32, 28, 28), kernelShape=(3, 32, 5, 5), w=layer1_w) # --------------------< Construction of Training Function >-------------------- with open(weightFile, 'rb') as w: dataset = cPickle.load(w) (param1, param2, param3, param4, param5, param6, param7) = dataset layer1_w.set_value(param1) layer1_b.set_value(param2) layer4_w.set_value(param3) layer4_b.set_value(param4) layer7_w.set_value(param5) layer7_b.set_value(param6) print "Epoch : " + str(param7) print "Pretrained weights were loaded!" # Define symbolic index variable index = T.iscalar('index') # Definiton of the symbolic function computing the prediction prediction = function( [index], numOfWrongClass, givens={ x: images[index:index+1], y: labels[index:index+1] } ) # Compute the testing error predictionError = prediction(imageId) print "The class of the predicted image is true or false :" + str(predictionError) return (layer0.eval({x: images.get_value()[imageId:imageId+1]}), layer1.eval({x: images.get_value()[imageId:imageId + 1]}), layer8.eval({x: images.get_value()[imageId:imageId + 1]}), delayer0.eval({x: images.get_value()[imageId:imageId + 1]}))
def run(subBatchSize=500, maxEpochNum=100, eta=0.1, trainErrPeriod=5, testErrPeriod=10, logfile='./log.txt', saveWeightFile = None, loadWeightFile=None): loadweight = True my = myCNN() # Read dataset base = './datasets/cifar10' trainSet_dir = base + '/' train_filename = ('data_batch_1','data_batch_2','data_batch_3','data_batch_4','data_batch_5') (trainImages, trainLabels) = load_cifar_dataset(trainSet_dir, train_filename) testSet_dir = base + '/' test_filename = ('test_batch',) (testImages, testLabels) = load_cifar_dataset(testSet_dir, test_filename) # Get the number of images in the training set numOfTrainImages = trainImages.get_value().shape[0] # Get the number of images in the test set numOfTestImages = testImages.get_value().shape[0] # Get the sub batch size for training set assert (numOfTrainImages % subBatchSize == 0), "The subbatch size must be a divisor of the number of train images" numOfTrainSubBatches = numOfTrainImages / subBatchSize # Get the sub batch size for test set assert (numOfTestImages % subBatchSize == 0), "The subbatch size must be a divisor of the number of test images" numOfTestSubBatches = numOfTestImages / subBatchSize x = T.matrix('x') # data input symbolic variable y = T.ivector('y') # labels symbolic variable # -----< Construction of Network Model >----- layer0 = x.reshape((subBatchSize, 3, 32, 32)) [layer1, layer1_w, layer1_b] = my.convolutionLayer(featureMaps=layer0, featureMapShape=(subBatchSize, 3, 32, 32), kernelShape=(32, 3, 5, 5), bias=0.1 ) layer2 = my.maxPoolingLayer(featureMaps=layer1, poolingShape=(2, 2), stride=2 ) layer3 = my.reLuLayer(featureMaps=layer2) [layer4, layer4_w, layer4_b] = my.convolutionLayer(featureMaps=layer3, featureMapShape=(subBatchSize, 32, 14, 14), kernelShape=(50, 32, 5, 5) ) layer5 = my.maxPoolingLayer(featureMaps=layer4, poolingShape=(2, 2), stride=2 ) layer6 = my.reLuLayer(featureMaps=layer5) [layer7, layer7_w, layer7_b] = my.convolutionLayer(featureMaps=layer6, featureMapShape=(subBatchSize, 50, 5, 5), kernelShape=(64, 50, 5, 5) ) layer8 = my.maxPoolingLayer(featureMaps=layer7, poolingShape=(1, ), stride=2 ) layer8, maxlocsLayer8 = my.maxPoolingLayer(featureMaps=layer7, featureMapShape=(subBatchSize,64, 1, 1), poolingSize=1) layer9 = my.reLuLayer(featureMaps=layer8) layer10 = layer9.flatten(2) layer13 = layer10.reshape((subBatchSize, 1*1*64)) [error, numOfWrongClass, layer14_w, layer14_b] = my.softmaxLayer(inputVect=layer13, labels=y, inputDim=1*1*64, numOfClasses=10 ) # --------------------< Construction of Training Function >-------------------- if loadweight is True and loadWeightFile is not None: with open(loadWeightFile, 'rb') as w: dataset = cPickle.load(w) (param1, param2, param3, param4, param5, param6, param7) = dataset layer1_w.set_value(param1) layer1_b.set_value(param2) layer4_w.set_value(param3) layer4_b.set_value(param4) layer7_w.set_value(param5) layer7_b.set_value(param6) print "Epoch : " + str(param7) loadweight = False print "Pretrained weights were loaded!" # Define symbolic index variable index = T.iscalar('index') # Define parameters params = [layer1_w, layer1_b, layer4_w, layer4_b, layer14_w, layer14_b] # Take the derivative of error function with respect to parameters grads = T.grad(cost=error, wrt=params) # Define updates updates = [(w, w - eta * delta) for w, delta in zip(params, grads)] # Definition of symbolic training function training = function([index],error, givens={ x: trainImages[index * subBatchSize: (index + 1) * subBatchSize], y: trainLabels[index * subBatchSize: (index + 1) * subBatchSize] }, updates=updates, ) # Definiton of the symbolic function computing the training error computeTrainingError = function( [index], numOfWrongClass, givens={ x: trainImages[index * subBatchSize: (index + 1) * subBatchSize], y: trainLabels[index * subBatchSize: (index + 1) * subBatchSize] } ) # Definiton of the symbolic testing function testing = function( [index], numOfWrongClass, givens={ x: testImages[index * subBatchSize: (index + 1) * subBatchSize], y: testLabels[index * subBatchSize: (index + 1) * subBatchSize] } ) print "The total number of training images in the dataset : " + str(numOfTrainImages) print "The total number of test images in the dataset : " + str(numOfTestImages) # Log file with open(logfile, "a") as logf: logf.write('The total number of training images in the dataset : ' + str(numOfTrainImages) + '\n') logf.write('The total number of test images in the dataset : ' + str(numOfTestImages) + '\n') minTrainErr = numOfTrainImages minTestErr = numOfTestImages for epoch in range(1, maxEpochNum+1): for subBatchIndex in range(numOfTrainSubBatches): err = training(subBatchIndex) if (epoch % trainErrPeriod == 0) or (epoch == 1): # Compute the training error trainingError = [computeTrainingError(inx) for inx in range(numOfTrainSubBatches)] # Get the total wrong classified number of elements in the training set totalWrongClass = np.sum(trainingError) print "Epoch : " + str(epoch) + " Training error : %" + str(totalWrongClass*100.0 / numOfTrainImages) + " " + str(totalWrongClass) # Write log file with open(logfile, "a") as logf: logf.write('Epoch : ' + str(epoch) + '\n') logf.write('Training : ' + str(totalWrongClass*100.0 / numOfTrainImages) + ' ' + str(totalWrongClass) + '\n') if (epoch % testErrPeriod == 0) or (epoch == 1): # Compute the testing error testingError = [testing(inx) for inx in range(numOfTestSubBatches)] # Get the total wrong classified number of elements in the test set totalTestWrongClass = np.sum(testingError) print "\t\t Testing error : %" + str(totalTestWrongClass*100.0 / numOfTestImages) + " " + str(totalTestWrongClass) # Write log file with open(logfile, "a") as logf: logf.write('Testing : ' + str(totalTestWrongClass * 100.0 / numOfTestImages) + ' ' + str(totalTestWrongClass) + '\n') if minTrainErr > totalWrongClass and saveWeightFile is not None: print "Weights are saved!" minTrainErr = totalWrongClass with open(saveWeightFile, 'wb') as w: cPickle.dump((layer1_w.get_value(), layer1_b.get_value(), layer4_w.get_value(), layer4_b.get_value(), layer7_w.get_value(), layer7_b.get_value(), epoch), w, protocol=cPickle.HIGHEST_PROTOCOL)
def run(subBatchSize=5, index=0, loadWeightFile=None): loadweight = True my = myCNN() myd = myDECNN() # Read dataset base = 'C:/Users/Abdulkadir/Desktop/Datasets/Pet Dataset/10class' dir = base + '/' filename = ('subset1.pkl', ) (images, labels) = load_pet_dataset(dir, filename) x = T.matrix('x') # data input symbolic variable y = T.ivector('y') # labels symbolic variable # -----< Construction of Network Model >----- layer0 = x.reshape((subBatchSize, 64, 64, 3)).transpose(0, 3, 1, 2) [layer1, layer1_w, layer1_b] = my.convolutionLayer(featureMaps=layer0, featureMapShape=(subBatchSize, 3, 64, 64), kernelShape=(16, 3, 11, 11), bias=0.1) layer2, maxlocsLayer2 = myd.maxPoolingLayer(featureMaps=layer1, featureMapShape=(subBatchSize, 16, 54, 54), poolingSize=3) layer3 = my.reLuLayer(featureMaps=layer2) [layer4, layer4_w, layer4_b ] = my.convolutionLayer(featureMaps=layer3, featureMapShape=(subBatchSize, 32, 18, 18), kernelShape=(32, 16, 7, 7)) layer5, maxlocsLayer5 = myd.maxPoolingLayer(featureMaps=layer4, featureMapShape=(subBatchSize, 32, 12, 12), poolingSize=2) layer6 = my.reLuLayer(featureMaps=layer5) # Set the other kernels to 0 for i in range(15): T.set_subtensor(layer6[i * 3:(i + 1) * 3, :2 * i, :, :], 0) T.set_subtensor(layer6[i * 3:(i + 1) * 3, 2 * i:, :, :], 0) delayer5 = myd.reLuLayer(featureMaps=layer6) delayer4 = myd.maxUnpoolingLayer(input=delayer5, featureMapShape=(subBatchSize, 32, 12, 12), maxLocations=maxlocsLayer5) delayer3, delayer3_w = myd.deconvolutionLayer( featureMaps=delayer4, featureMapShape=(subBatchSize, 32, 12, 12), kernelShape=(16, 32, 7, 7), w=layer4_w) delayer2 = myd.reLuLayer(featureMaps=delayer3) delayer1 = myd.maxUnpoolingLayer(input=delayer2, featureMapShape=(subBatchSize, 16, 54, 54), maxLocations=maxlocsLayer2) delayer0, delayer0_w = myd.deconvolutionLayer( featureMaps=delayer1, featureMapShape=(subBatchSize, 16, 54, 54), kernelShape=(3, 32, 11, 11), w=layer1_w) # --------------------< Construction of Training Function >-------------------- if loadweight is True and loadWeightFile is not None: with open(loadWeightFile, 'rb') as w: dataset = pickle.load(w) (param1, param2, param3, param4, param5, param6, param7, param8) = dataset layer1_w.set_value(param1) layer1_b.set_value(param2) layer4_w.set_value(param3) layer4_b.set_value(param4) loadweight = False print "Pretrained weights were loaded!" # Define symbolic index variable index = T.iscalar('index') return (layer0.eval({x: images.get_value()[0:subBatchSize]}), delayer0.eval({x: images.get_value()[0:subBatchSize]}))
print('model file: {0}'.format(model_filepath), file=sys.stderr) print('', file=sys.stderr) # 学習データの読み込み labels, imgfiles, labeldict, labelnames = read_image_list( IMAGE_LIST, DATA_DIR) # 情報を一時ファイルに保存(学習後,性能テスト時に使用する) n_classes = len(labeldict) # クラス数 with open(MODEL_DIR + 'labeldict.pickle', 'wb') as f: pickle.dump(labeldict, f) # ラベル名からラベル番号を与える辞書をファイルに保存 with open(MODEL_DIR + 'labelnames.pickle', 'wb') as f: pickle.dump(labelnames, f) # ラベル番号からラベル名を与える配列をファイルに保存 # ネットワークモデルの作成 cnn = myCNN(WIDTH, HEIGHT, CHANNELS, n_classes) ### ここから下は,必要に応じて書き換えると良い ### # 追加条件の指定 conditions = {} conditions['in_channels'] = CHANNELS # 入力画像のチャンネル数が 1(グレースケール画像) # 学習処理 cnn = train( device=dev, # 使用するGPUのID,変えなくて良い model_dir=MODEL_DIR, # 一時ファイルを保存するフォルダ,変えなくて良い in_data=imgfiles, # モデルの入力データ,今回はMNIST画像 out_data=labels, # モデルの出力データ,今回はクラスラベル model=cnn, # 学習するネットワークモデル loss_func=nn.CrossEntropyLoss(), # 損失関数,今回は softmax cross entropy