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()
class Generator: 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 load_data(self): with open(self.folder_label, 'r') as f: for line in f.readlines(): if ';' in line: image_file, txt = line.split(sep=';', maxsplit=1) image_file = os.path.abspath(os.path.join(self.folder_image, image_file)) txt = txt.strip() if os.path.isfile(image_file): self.examples.append((txt, image_file)) def examples_generator(self): random.shuffle(self.examples) while True: images, target, encode_hidden = [], [], [] for i in range(self.batch_size): self.cur_index += 1 if self.cur_index >= len(self.examples): self.cur_index = 0 txt, img_path = self.examples[self.cur_index] images.append(self.image_util.load(img_path)) target.append(self.vocab.one_hot_encode(txt)) # print(self.vocab.text_to_labels(txt)) # print(self.vocab.labels_to_text(target[0])) yield np.array(images), np.array(target)
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'])
class ImageUtilityTest(unittest.TestCase): """Test to see if all the operations can be performed on the given file path.""" def setUp(self): module_logger.debug('method setUp was called') self.img = ImageUtil() module_logger.debug('method setUp completed successfully') def testImageLocation(self): """Testing for correct imagefile name extraction from url """ module_logger.debug('method testImageLocation was called') name = '/name.png' ## Possible outcomes outcomes = ['name.png', 'name.png.png', 'name.png.jpg', \ 'name.png.gif', 'name.jpg', 'name.jpg.png', 'name.jpg.jpg', 'name.jpg.gif', \ 'name.gif', 'name.gif.png', 'name.gif.jpg', 'name.gif.gif'] ## Generate a SET of different kind of names and then verify that ## they all give the expected output chars='~!@#$%^&*()_+-=`[]{}\|;:"<>,./?\'0jpngif' #chars='abcdefghi' for i in range(4): name = chars[ random.randrange(len(chars)) ] + name ## The name is of format /____name.jpg**** for char1 in chars: for char2 in chars: for char3 in chars: for char4 in chars: self.assertIn(\ self.img.getImageFileName(name + char1 + char2 + char3 + char4), outcomes) name = 'name.png' for i in range(4): name = name + chars[ random.randrange(len(chars)) ] name = '/' + name ## name is of format /****name.jpg____ for char1 in chars: for char2 in chars: for char3 in chars: for char4 in chars: self.assertIn(\ self.img.getImageFileName(char1 + char2 + char3 + char4 + name), outcomes) module_logger.debug('method testImageLocation completed successfully') def testImageName(self): """Testing for correct image extensions""" module_logger.debug('method testImageName was called') self.assertEqual( self.img.getImageType('*.JPG'), 'jpg', 'JPG test failed') self.assertEqual( self.img.getImageType('*.PNG'), 'png', 'PNG test failed') self.assertEqual( self.img.getImageType('*.GIF'), 'gif', 'GIF test failed') self.assertEqual( self.img.getImageType('***'), 'unknown Image Type', 'UNKNOWN IMAGE TYPE test failed') module_logger.debug('method testImageName completed successfully') def testImageConversion(self): """Testing for format conversion to JPG""" module_logger.debug('method testImageConversion was called') ## Change the directory to get correct images currentDir = getcwd() workingDir = path.dirname( path.abspath(__file__) ) chdir(workingDir) ## Convert different formats to jpg self.img.convertImageToJpg("JPEG.jpeg", "JPEG.jpg") self.img.convertImageToJpg("PNG.png","PNG.jpg") self.img.convertImageToJpg("JPG.jpg", "JPG.jpg") self.img.convertImageToJpg("GIF.gif", "GIF.jpg") ## Check if the converted files are there self.assertTrue( path.isfile("JPEG.jpg") ) self.assertTrue( path.isfile("PNG.jpg") ) self.assertTrue( path.isfile("JPG.jpg") ) self.assertTrue( path.isfile("GIF.jpg") ) ## Delete the converted files remove('JPEG.jpg') remove('PNG.jpg') remove('GIF.jpg') chdir(currentDir) module_logger.debug('method testImageConversion completed successfully') def runTest(self): filePointer = open('Test-Results.txt', 'a') suite = unittest.TestLoader().loadTestsFromTestCase(ImageUtilityTest) result = unittest.TextTestRunner(verbosity=2, stream=filePointer).run(suite) filePointer.close() return result
def setUp(self): module_logger.debug('method setUp was called') self.img = ImageUtil() module_logger.debug('method setUp completed successfully')
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)