def train(n, k, firstRun, epochs_no, starting_epoch_no, last_model_file = None): if firstRun: total_training_batches_no = b.divide_into_files(True) total_test_batches_no = b.divide_into_files(False) else: total_training_batches_no = 707 total_test_batches_no = 177 cvX, cvY1, cvY2, cvY3 = b.load_cross_validation() # put appropriate Y value here depending on the category you want to run for cvY = tflearn.data_utils.to_categorical(cvY3, b.CLASS_3_NUMBER) # Training model = buildNetwork(n, k) if (starting_epoch_no > 0): model.load(b.PATH + last_model_file) for j in range(starting_epoch_no, epochs_no): validation_set = None snapshot_epoch = False for i in range(0, total_training_batches_no): X, Y, Y1, Y2 = b.load_batch(i, True) Y = tflearn.data_utils.to_categorical(Y, b.CLASS_3_NUMBER) if i == total_training_batches_no - 1 or i % 50 == 0: validation_set = cvX, cvY snapshot_epoch = True model.fit(X, Y, n_epoch=1, validation_set=validation_set, snapshot_epoch=snapshot_epoch, show_metric=True, run_id='wideresnet') if i % 50 == 0: model.save(b.PATH + 'ALLmodel_wideresnet_batch_' + str(i) + 'epoch_' + str(j) + '.txt') model.save(b.PATH + 'ALLmodel_wideresnet_epoch_' + str(j) + '.txt') return [model, total_test_batches_no]
def test(model, total_test_batches_no): predictions = [] for i in range(0, total_test_batches_no): testX, product_id, Y3, Y4 = b.load_batch(i, False) size = len(testX) / 10 for i in range(0, 10): testXpart = testX[i * size, min((i + 1) * size, len(testX))] predictions.extend(model.predict_label(testXpart)) with open(b.PATH + 'ALL' + 'resNextPredictions.txt', 'wb') as pickleFile: pickle.dump(predictions, pickleFile)
net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001) #resnet part net = residual_block(net, n, 16, downsample=True) net = residual_block(net, 1, 32, downsample=True) net = residual_block(net, n-1, 32, downsample=True) net = residual_block(net, 1, 64, downsample=True) net = residual_block(net, n-1, 64) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, b.CLASS_3_NUMBER, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') # Training model = tflearn.DNN(net, tensorboard_verbose=0, tensorboard_dir=b.PATH + 'log') model.load(b.PATH + last_model_file) predictions = [] for i in range(0, test_batches_no): testX, product_id, Y3, Y4 = b.load_batch(i, False) size = len(testX) / 10 for i in range(0, 10): testXpart = testX[i * size, min((i + 1) * size, len(testX))] predictions.extend(model.predict_label(testXpart)) with open(b.PATH + 'ALL' + str(n) + 'resNetPredictions.txt', 'wb') as pickleFile: pickle.dump(predictions, pickleFile)
net = wideresnet_block(net, 1, 64, 2, downsample=True) net = wideresnet_block(net, n-1, 64, k, downsample=True) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, b.CLASS_3_NUMBER, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') # Training model = tflearn.DNN(net, tensorboard_verbose=0, clip_gradients=0., tensorboard_dir=b.PATH + 'log') if (starting_epoch_no > 0): model.load(b.PATH + last_model_file) for j in range(starting_epoch_no, epochs_no): validation_set = None snapshot_epoch = False for i in range(0, training_batches_no): X, Y, Y1, Y2 = b.load_batch(i, True) Y = tflearn.data_utils.to_categorical(Y, b.CLASS_3_NUMBER) if i == training_batches_no - 1 or i % 50 == 0: validation_set = cvX, cvY snapshot_epoch = True model.fit(X, Y, n_epoch=1, validation_set=validation_set, snapshot_epoch=snapshot_epoch, show_metric=True, run_id='wideresnet') if i % 50 == 0: model.save(b.PATH + 'ALLmodel_wideresnet_batch_' + str(i) + 'epoch_' + str(j) + '.txt') model.save(b.PATH + 'ALLmodel_wideresnet_epoch_' + str(j) + '.txt')
net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') with open(b.PATH + 'ALLResNetNet created.txt', 'wb') as pickleFile: pickle.dump([], pickleFile) # Training model = tflearn.DNN(net, tensorboard_verbose=0, tensorboard_dir=b.PATH + 'log') for j in range(0, epochs_no): with open(b.PATH + 'ALL' + str(j) + 'ResNetEpoch.txt', 'wb') as pickleFile: pickle.dump([], pickleFile) validation_set = None snapshot_epoch = False for i in range(0, training_batches_no): with open(b.PATH + 'ALL' + str(i) + 'ResNetBatch.txt', 'wb') as pickleFile: pickle.dump([], pickleFile) X, Y, Y1, Y2 = b.load_batch(i, True) Y = tflearn.data_utils.to_categorical(Y, b.CLASS_3_NUMBER) if i == training_batches_no - 1 or i % 50 == 0: validation_set = cvX, cvY snapshot_epoch = True model.fit(X, Y, n_epoch=1, validation_set=validation_set, snapshot_epoch=snapshot_epoch, show_metric=True, run_id='resnet') if i % 50 == 0: model.save(b.PATH + 'ALLmodel_resnet_batch_' + str(i) + 'epoch_' + str(j) + '.txt')
model = build_network() after_first_run_flag= False val_x, cat1_y, cate2_y, cate3_y = load_cross_validation() # *************************************************************************** for epoch in range(0, EPOCH_NUM ): for batch_index in range(0, total_train_batch_file): if (after_first_run_flag): model.load( MODEL_NAME ) after_first_run_flag = True val_set = None # We need to change here for label selection based on category train_data, train_labels, Y2, Y3 = load_batch(batch_index, True) #converting Y list into Matrix train_labels = to_categorical( train_labels, CLASS_NUMBER) val_y_labels = to_categorical( cate3_y, CLASS_NUMBER) # We need to change here if (batch_index == total_train_batch_file-1 ): val_set =(val_x, val_y_labels) model.fit( train_data, train_labels, n_epoch= 1 , shuffle=True, validation_set= val_set , show_metric=True, batch_size=96, run_id=MODEL_NAME ) # We need to change here model.save( MODEL_NAME ) print ("Finished load: ", batch_index ) print ("Finished epoch: ", epoch )