def train_top_model(): # load top model architecture top_model, _, _ = create_top_model(num_classes=500) top_model.compile(optimizer=optimizers.Adam(), loss='categorical_crossentropy', metrics=['accuracy',metrics.top_k_categorical_accuracy]) best_val_loss = 100 f = open('console_dumps/{}.txt'.format(exp_name),'w') unpickled_train = pickle.load(open('cache/parts_train.p','rb')) unpickled_valid = pickle.load(open('cache/parts_validation.p','rb')) #unpickled_test = pickle.load(open('cache/parts_test.p','rb')) for i in range(epochs): time_start = time.time() train_generator = utils.img_parts_generator(part_file_name, train_data_dir, batch_size=5000, bottleneck_file='bottleneck/bottleneck_60_train.npy', unpickled=unpickled_train) val_generator = utils.img_parts_generator(part_file_name, validation_data_dir, batch_size=5000, bottleneck_file='bottleneck/bottleneck_60_validation.npy', unpickled=unpickled_valid) j = 0 train_eval = [] print('Training') for inp, label in train_generator: hist = top_model.fit(inp, label, verbose=0, batch_size=400) res = [hist.history['loss'][0], hist.history['acc'][0]] train_eval.append(res) #print("Epoch: {}/{} Batch (train): {}/{} train_l: {:.4f} train_acc: {:.4f}".format(i+1,epochs,j+1,42320/batch_size, res[0], res[1])) j += 1 # find mean of loss and acc train_eval = np.mean(train_eval, axis=0) val_eval = [] j = 0 print('Evaluating validation set') for inp, label in val_generator: res = top_model.evaluate(inp, label, verbose=0, batch_size=400) #print("Epoch: {}/{} Batch (valid): {}/{} val_l: {:.4f} val_acc: {:.4f}".format(i+1,epochs,j+1,3000/batch_size, res[0], res[1])) val_eval.append(res) j += 1 if j==5: break val_eval = np.mean(val_eval, axis=0) if val_eval[0] < best_val_loss: #print('Saving weights') best_val_loss = val_eval[0] top_model.save_weights('models/{}_{}_{:.4f}.hdf5'.format(exp_name,i+1,round(best_val_loss))) time_taken = time.time() - time_start log = 'Epoch: {}, train_l: {:.4f}, train_a: {:.4f}, val_l: {:.4f}, val_a: {:.4f}, time: {:.4f}\n'.format(i+1,train_eval[0], train_eval[1], val_eval[0],val_eval[1], time_taken) f.write(log) print(log) f.close()
def train_top_model(): # get mapping of label id (0...499) to label string label_mapping = utils.get_label_mapping_from_file('train_labels.txt') # load bottlenecks print('Train') train_data = np.load(open('bottleneck/bottleneck_features_train.npy', 'rb')) print(train_data.shape) train_labels = utils.get_labels_from_file('train_labels.txt') print(len(train_labels)) print('Validation') validation_data = np.load(open('bottleneck/bottleneck_features_validation.npy', 'rb')) print(validation_data.shape) val_size_per_class = 6 validation_labels = get_fixed_labels(val_size_per_class) print('Test') test_data = np.load(open('bottleneck/bottleneck_features_test.npy', 'rb')) print(test_data.shape) test_size_per_class = 9 test_labels = get_fixed_labels(test_size_per_class) epochs = 20 tensorboard_callback = TensorBoard(log_dir='./logs/top_model/', histogram_freq=0, write_graph=True, write_images=False) checkpoint_callback = ModelCheckpoint('./models/top_model_weights.{epoch:02d}-{val_acc:.2f}.hdf5', monitor='val_acc', verbose=0, save_best_only=True, save_weights_only=False, mode='auto') # load top model architecture top_model, x, top_model_inputs = create_top_model() top_model.compile(optimizer=optimizers.Adam(), loss='categorical_crossentropy', metrics=['accuracy']) history_model = top_model.fit(train_data, train_labels, epochs=epochs, batch_size=batch_size, verbose=1, validation_data=(validation_data, validation_labels), callbacks = [tensorboard_callback, checkpoint_callback]) pandas.DataFrame(history_model.history).to_csv('./history/top_model.csv') ev = top_model.evaluate(test_data,test_labels, batch_size=batch_size, verbose=1) print(ev)
def train_top_model(): exp_name = 'top_cropped' # load bottlenecks print('Train') train_data = np.load(open('bottleneck/cropped_60_train.npy', 'rb')) print(train_data.shape) train_labels = utils.get_labels_from_file('train_labels.txt') print(len(train_labels)) print('Validation') validation_data = np.load( open('bottleneck/cropped_60_validation.npy', 'rb')) print(validation_data.shape) val_size_per_class = 6 validation_labels = get_fixed_labels(val_size_per_class) epochs = 20 tensorboard_callback = TensorBoard(log_dir='./logs/' + exp_name + '/', histogram_freq=0, write_graph=True, write_images=False) checkpoint_callback = ModelCheckpoint('./models/' + exp_name + '.{epoch:02d}-{val_acc:.2f}.hdf5', monitor='val_acc', verbose=0, save_best_only=True, save_weights_only=False, mode='auto') # load top model architecture top_model, x, top_model_inputs = create_top_model() top_model.compile(optimizer=optimizers.Adam(), loss='categorical_crossentropy', metrics=['accuracy', metrics.top_k_categorical_accuracy]) history_model = top_model.fit( train_data, train_labels, epochs=epochs, batch_size=batch_size, verbose=1, validation_data=(validation_data, validation_labels), callbacks=[tensorboard_callback, checkpoint_callback])
def train_top_model(): # load top model architecture top_model, _, _ = create_top_model( num_classes=4, activation=None, weights='models/v2_top_bb_30_28.0000.hdf5') top_model.compile(optimizer=optimizers.Adam(), loss='mean_absolute_error', metrics=['accuracy']) print(top_model.summary()) bottlenecks_train = utils.load_bottlenecks(bottleneck_dir + 'bottleneck_60_train.npy') bottlenecks_valid = utils.load_bottlenecks(bottleneck_dir + 'bottleneck_60_validation.npy') best_val_loss = 100 f = open('console_dumps/{}.txt'.format(exp_name), 'w') unpickled_train = pickle.load(open('cache/bb_train.p', 'rb')) unpickled_valid = pickle.load(open('cache/bb_validation.p', 'rb')) for i in range(epochs): time_start = time.time() train_generator = utils.img_parts_generator( part_file_name, 'train/', batch_size=5000, bottlenecks=bottlenecks_train, unpickled=unpickled_train, bb_only=True) val_generator = utils.img_parts_generator( part_file_name, 'validation/', batch_size=5000, bottlenecks=bottlenecks_valid, unpickled=unpickled_valid, bb_only=True) train_eval = [] # stores metrics of training iterations # print('Training') for inp, label in train_generator: hist = top_model.fit(inp, label, verbose=0, batch_size=400) res = [hist.history['loss'][0], hist.history['acc'][0]] train_eval.append(res) # find mean of loss and acc train_eval = np.mean(train_eval, axis=0) val_eval = [] # stores metrics of validation iterations j = 0 # print('Evaluating validation set') for inp, label in val_generator: res = top_model.evaluate(inp, label, verbose=0, batch_size=400) #print("Epoch: {}/{} Batch (valid): {}/{} val_l: {:.4f} val_acc: {:.4f}".format(i+1,epochs,j+1,3000/batch_size, res[0], res[1])) val_eval.append(res) j += 1 #if j==5: # break val_eval = np.mean(val_eval, axis=0) # save weights if current loss beats best loss if val_eval[0] < best_val_loss: #print('Saving weights') best_val_loss = val_eval[0] top_model.save_weights('models/{}_{}_{:.4f}.hdf5'.format( exp_name, i + 1, round(best_val_loss))) time_taken = time.time() - time_start log = 'Epoch: {}, train_l: {:.4f}, train_a: {:.4f}, val_l: {:.4f}, val_a: {:.4f}, time: {:.4f}\n'.format( i + 1, train_eval[0], train_eval[1], val_eval[0], val_eval[1], time_taken) f.write(log) print(log) f.close()