def __init__(self, folder_image, folder_label): self.folder_image = folder_image self.folder_label = folder_label self.batch_size = BATCH_SIZE self.max_txt_length = max_txt_length self.examples = [] self.cur_index = 0 self.load_data() self.image_util = ImageUtil(image_height=image_height, image_width=image_width) self.vocab = Vocabulary()
def main(): sess = tf.Session() image_util = ImageUtil() dataloader = ImageDataLoader(IMAGES_DIR, image_util) train_dataset = dataloader.get_dataset('train', NUM_TRAIN_IMAGES) val_dataset = dataloader.get_dataset('val', NUM_VAL_IMAGES) test_dataset = dataloader.get_dataset('test', NUM_TEST_IMAGES) model = ImageColorization().model model.compile(optimizer=tf.train.AdamOptimizer(), loss={'colorization_output': 'mean_squared_error', 'classification_output': 'categorical_crossentropy'}, loss_weights={'colorization_output': 1., 'classification_output': 1./300}) print 'Starting training' for i in range(NUM_EPOCHS): model.fit(train_dataset['greyscale'], {'colorization_output': train_dataset['color'], 'classification_output': train_dataset['class']}, epochs=1) ab_unscaled = model.predict(val_dataset['greyscale'])[0] lab = np.concatenate([val_dataset['greyscale']*100, ab_unscaled*200-100], axis=3) show_predicted_values(ab_unscaled, lab) print 'Finished training' results = model.predict(test_dataset['greyscale'])
cv2.imshow('origin', img) def visual_attention(result, attention_plot): len_result = len(result) for i in range(len_result): show_origin_image() temp_att = np.reshape(attention_plot[i], (height, width)) cv2.imshow(f'predict word: {result[i]}', temp_att) cv2.waitKey() cv2.destroyAllWindows() if __name__ == '__main__': image_util = ImageUtil(image_height=image_height, image_width=image_width) img_tensor = image_util.load(args.image) img_tensor = np.expand_dims(img_tensor, 0) result = '' hidden = tf.zeros((1, decode_units)) word_one_hot = np.zeros((1, vocab_size)) word_one_hot[0][1] = 1. attention_plot = np.zeros((max_txt_length, height * width)) for i in range(max_txt_length): predict, hidden, attention_weights = model(word_one_hot, hidden, img_tensor) predict_id = tf.argmax(predict, axis=-1)