├── image.png ├── mask.png ├── tiger.jpg ├── tf_dataset_samples ├── data │ ├── 01.jpg │ ├── 02.jpg │ ├── 03.jpg │ ├── 04.jpg │ ├── 05.jpg │ └── 06.jpg ├── train.csv ├── ds_generator.py ├── ds_numpy.py └── ds_tensor.py ├── plot_chinese_PIL_ImageDraw.py ├── plot_chinese_cv2.py ├── plot_chinese_matplotlib.py ├── xor_with_tensorflow.py ├── convert_to_pascal_voc.py ├── README.md ├── fizzbuzz_with_tensorflow.py ├── plot_mask_on_image.py ├── convert_to_detectron_json.py ├── visualize_outputs_keras_model2.py └── visualize_outputs_keras_model1.py /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kongsea/yueye/HEAD/image.png -------------------------------------------------------------------------------- /mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kongsea/yueye/HEAD/mask.png -------------------------------------------------------------------------------- /tiger.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kongsea/yueye/HEAD/tiger.jpg -------------------------------------------------------------------------------- /tf_dataset_samples/data/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kongsea/yueye/HEAD/tf_dataset_samples/data/01.jpg -------------------------------------------------------------------------------- /tf_dataset_samples/data/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kongsea/yueye/HEAD/tf_dataset_samples/data/02.jpg -------------------------------------------------------------------------------- /tf_dataset_samples/data/03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kongsea/yueye/HEAD/tf_dataset_samples/data/03.jpg -------------------------------------------------------------------------------- /tf_dataset_samples/data/04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kongsea/yueye/HEAD/tf_dataset_samples/data/04.jpg -------------------------------------------------------------------------------- /tf_dataset_samples/data/05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kongsea/yueye/HEAD/tf_dataset_samples/data/05.jpg -------------------------------------------------------------------------------- /tf_dataset_samples/data/06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kongsea/yueye/HEAD/tf_dataset_samples/data/06.jpg -------------------------------------------------------------------------------- /tf_dataset_samples/train.csv: -------------------------------------------------------------------------------- 1 | data/01.jpg,猫 2 | data/05.jpg,狗 3 | data/03.jpg,猫 4 | data/04.jpg,狗 5 | data/06.jpg,狗 6 | data/02.jpg,猫 -------------------------------------------------------------------------------- /plot_chinese_PIL_ImageDraw.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import PIL.Image as Image 4 | import PIL.ImageDraw as ImageDraw 5 | import PIL.ImageFont as ImageFont 6 | 7 | image_name = 'tiger' 8 | image = Image.open('{}.jpg'.format(image_name)) 9 | 10 | color = 'red' 11 | (left, top, right, bottom) = (270, 120, 420, 370) 12 | class_name = '老虎' 13 | 14 | try: 15 | font = ImageFont.truetype('wqy-microhei.ttc', 14) 16 | except IOError: 17 | font = ImageFont.load_default() 18 | 19 | draw = ImageDraw.Draw(image) 20 | draw.line([(left, top), (left, bottom), (right, bottom), 21 | (right, top), (left, top)], width=1, fill=color) 22 | 23 | draw.text( 24 | (left, top-14), 25 | class_name.decode('utf-8'), 26 | fill='black', 27 | font=font) 28 | 29 | image.show() 30 | -------------------------------------------------------------------------------- /plot_chinese_cv2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import cv2 4 | import numpy as np 5 | import PIL.Image as Image 6 | import PIL.ImageDraw as ImageDraw 7 | import PIL.ImageFont as ImageFont 8 | 9 | image_name = 'tiger' 10 | im = cv2.imread('{}.jpg'.format(image_name)) 11 | im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) 12 | image = Image.fromarray(im) 13 | 14 | color = 'red' 15 | (left, top, right, bottom) = (270, 120, 420, 370) 16 | class_name = '老虎' 17 | 18 | try: 19 | font = ImageFont.truetype('wqy-microhei.ttc', 18) 20 | except IOError: 21 | font = ImageFont.load_default() 22 | 23 | draw = ImageDraw.Draw(image) 24 | draw.line([(left, top), (left, bottom), (right, bottom), 25 | (right, top), (left, top)], width=1, fill=color) 26 | 27 | draw.text( 28 | (left, top-14), 29 | class_name.decode('utf-8'), 30 | fill='black', 31 | font=font) 32 | 33 | im = np.array(image) 34 | im = im[:, :, ::-1].copy() 35 | 36 | cv2.imwrite('test.jpg', im) 37 | -------------------------------------------------------------------------------- /plot_chinese_matplotlib.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import random 4 | 5 | import cv2 6 | import matplotlib as mpl 7 | import matplotlib.pyplot as plt 8 | 9 | mpl.rcParams['font.sans-serif'] = ['SimHei'] 10 | 11 | 12 | image_name = 'tiger' 13 | im = cv2.imread('{}.jpg'.format(image_name)) 14 | im = im[:, :, (2, 1, 0)] 15 | _, ax = plt.subplots() 16 | ax.imshow(im, aspect='equal') 17 | ax.set_title(('detection result for {}').format(image_name), fontsize=14) 18 | 19 | bbox = [270, 120, 420, 370] 20 | score = 0.99 21 | 22 | color = (random.random(), random.random(), random.random()) 23 | ax.add_patch( 24 | plt.Rectangle((bbox[0], bbox[1]), 25 | bbox[2] - bbox[0], 26 | bbox[3] - bbox[1], fill=False, 27 | edgecolor=color, linewidth=1) 28 | ) 29 | class_name = '老虎' 30 | ax.text(bbox[0], bbox[1] - 2, 31 | u'{:s} {:.3f}'.format(class_name.decode('utf-8'), score), 32 | # unicode(class_name.decode('utf-8')) + str(score), 33 | bbox=dict(facecolor='blue', alpha=0.5), 34 | fontsize=14, color='white') 35 | 36 | plt.axis('off') 37 | plt.tight_layout() 38 | plt.draw() 39 | plt.show() 40 | -------------------------------------------------------------------------------- /tf_dataset_samples/ds_generator.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import time 4 | 5 | import cv2 6 | import tensorflow as tf 7 | 8 | 9 | label_map = {'猫': 0, '狗': 1} 10 | 11 | 12 | def gen(): 13 | with open('train.csv') as f: 14 | lines = [line.strip().split(',') for line in f.readlines()] 15 | 16 | index = 0 17 | while True: 18 | image = cv2.imread(lines[index][0]) 19 | image = cv2.resize(image, (224, 224)) 20 | label = label_map[lines[index][1]] 21 | yield (image, label) 22 | index += 1 23 | if index == len(lines): 24 | index = 0 25 | 26 | 27 | def create_dataset(): 28 | 29 | data = tf.data.Dataset.from_generator(gen, (tf.float32, tf.int32), 30 | (tf.TensorShape([224, 224, 3]), tf.TensorShape([]))) 31 | data = data.batch(2) 32 | data = data.make_one_shot_iterator() 33 | tt = time.time() 34 | _, labels = data.get_next() 35 | with tf.Session() as sess: 36 | for i in range(100): 37 | getlabels = sess.run(labels) 38 | print('{} -> {}'.format(i, getlabels)) 39 | print(time.time() - tt) 40 | 41 | 42 | def main(): 43 | create_dataset() 44 | 45 | 46 | if __name__ == '__main__': 47 | main() 48 | -------------------------------------------------------------------------------- /tf_dataset_samples/ds_numpy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import time 4 | 5 | import cv2 6 | import numpy as np 7 | import tensorflow as tf 8 | 9 | 10 | with open('train.csv') as f: 11 | lines = [line.strip().split(',') for line in f.readlines()] 12 | 13 | label_map = {'猫': 0, '狗': 1} 14 | 15 | images = np.ndarray((len(lines), 224, 224, 3), float) 16 | labels = np.ndarray((len(lines)), int) 17 | 18 | for i, line in enumerate(lines): 19 | image = cv2.imread(line[0]) 20 | image = cv2.resize(image, (224, 224)) 21 | label = label_map[line[1]] 22 | 23 | images[i] = image 24 | labels[i] = label 25 | 26 | batch_size = 2 27 | data = tf.data.Dataset.from_tensor_slices((images, labels)) 28 | data = data.batch(batch_size) 29 | iterator = tf.data.Iterator.from_structure(data.output_types, 30 | data.output_shapes) 31 | init_op = iterator.make_initializer(data) 32 | tt = time.time() 33 | with tf.Session() as sess: 34 | sess.run(init_op) 35 | for i in range(100): 36 | try: 37 | _, labels = iterator.get_next() 38 | labels = sess.run(labels) 39 | print('{} -> {}'.format(i, labels)) 40 | except tf.errors.OutOfRangeError: 41 | sess.run(init_op) 42 | 43 | print(time.time() - tt) 44 | -------------------------------------------------------------------------------- /xor_with_tensorflow.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import numpy as np 4 | import tensorflow as tf 5 | 6 | data = tf.placeholder(tf.float32, shape=(4, 2)) 7 | label = tf.placeholder(tf.float32, shape=(4, 1)) 8 | 9 | with tf.variable_scope('layer1') as scope: 10 | weight = tf.get_variable(name='weight', shape=(2, 2)) 11 | bias = tf.get_variable(name='bias', shape=(2,)) 12 | x = tf.nn.sigmoid(tf.matmul(data, weight) + bias) 13 | with tf.variable_scope('layer2') as scope: 14 | weight = tf.get_variable(name='weight', shape=(2, 1)) 15 | bias = tf.get_variable(name='bias', shape=(1,)) 16 | x = tf.matmul(x, weight) + bias 17 | 18 | preds = tf.nn.sigmoid(x) 19 | loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=label, logits=x)) 20 | learning_rate = tf.placeholder(tf.float32) 21 | optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) 22 | 23 | train_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) 24 | train_label = np.array([[0], [1], [1], [0]]) 25 | 26 | with tf.Session() as sess: 27 | sess.run(tf.global_variables_initializer()) 28 | for step in range(10000): 29 | if step < 3000: 30 | lr = 1 31 | elif step < 6000: 32 | lr = 0.1 33 | else: 34 | lr = 0.01 35 | _, l, pred = sess.run([optimizer, loss, preds], feed_dict={ 36 | data: train_data, label: train_label, learning_rate: lr}) 37 | if step % 500: 38 | print('Step: {} -> Loss: {} -> Predictions: {}'.format(step, l, pred)) 39 | -------------------------------------------------------------------------------- /convert_to_pascal_voc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import copy 4 | import os 5 | import xml.etree.cElementTree as ET 6 | 7 | import cv2 8 | 9 | template_file = 'anno.xml' 10 | target_dir = 'Annotations/' 11 | image_dir = 'JPEGImages/' 12 | anno_dir = 'annotations/' 13 | 14 | anno_files = [os.path.join(anno_dir, f) for f in os.listdir(anno_dir) if f.endswith('.txt')] 15 | 16 | for af in anno_files: 17 | with open(af) as f: 18 | anno_lines = [f.strip() for f in f.readlines()] 19 | 20 | image_file = af.rpartition('/')[-1].replace('txt', 'jpg') 21 | 22 | tree = ET.parse(template_file) 23 | root = tree.getroot() 24 | 25 | # filename 26 | root.find('filename').text = image_file 27 | # size 28 | sz = root.find('size') 29 | im = cv2.imread(image_dir + image_file) 30 | sz.find('height').text = str(im.shape[0]) 31 | sz.find('width').text = str(im.shape[1]) 32 | sz.find('depth').text = str(im.shape[2]) 33 | 34 | # object 35 | obj_ori = root.find('object') 36 | root.remove(obj_ori) 37 | 38 | for al in anno_lines: 39 | bb_info = al.split() 40 | 41 | x_1 = int(bb_info[1]) 42 | y_1 = int(bb_info[2]) 43 | x_2 = int(bb_info[3]) 44 | y_2 = int(bb_info[4]) 45 | 46 | obj = copy.deepcopy(obj_ori) 47 | 48 | obj.find('name').text = bb_info[0].decode('utf-8') 49 | bb = obj.find('bndbox') 50 | bb.find('xmin').text = str(x_1) 51 | bb.find('ymin').text = str(y_1) 52 | bb.find('xmax').text = str(x_2) 53 | bb.find('ymax').text = str(y_2) 54 | 55 | root.append(obj) 56 | 57 | xml_file = image_file.replace('jpg', 'xml') 58 | 59 | tree.write(target_dir + xml_file, encoding='utf-8', xml_declaration=True) 60 | 61 | print xml_file 62 | -------------------------------------------------------------------------------- /tf_dataset_samples/ds_tensor.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import time 4 | 5 | import tensorflow as tf 6 | 7 | label_map = {'猫': 0, '狗': 1} 8 | 9 | with open('train.csv') as f: 10 | lines = [line.strip().split(',') for line in f.readlines()] 11 | 12 | 13 | def _parse_function(filename, label): 14 | image_string = tf.read_file(filename) 15 | image_decoded = tf.image.decode_jpeg(image_string, channels=3) # (1) 16 | image = tf.cast(image_decoded, tf.float32) 17 | 18 | image = tf.image.resize_images(image, [224, 224]) # (2) 19 | return image, filename, label 20 | 21 | 22 | def training_preprocess(image, filename, label): 23 | flip_image = tf.image.random_flip_left_right(image) # (4) 24 | 25 | return flip_image, filename, label 26 | 27 | 28 | images = [] 29 | labels = [] 30 | for line in lines: 31 | images.append(line[0]) 32 | labels.append(label_map[line[1]]) 33 | 34 | images = tf.constant(images) 35 | labels = tf.constant(labels) 36 | images = tf.random_shuffle(images, seed=0) 37 | labels = tf.random_shuffle(labels, seed=0) 38 | data = tf.data.Dataset.from_tensor_slices((images, labels)) 39 | 40 | data = data.map(_parse_function, num_parallel_calls=4) 41 | data = data.prefetch(buffer_size=2 * 10) 42 | batched_data = data.batch(2) 43 | 44 | iterator = tf.data.Iterator.from_structure(batched_data.output_types, 45 | batched_data.output_shapes) 46 | 47 | init_op = iterator.make_initializer(batched_data) 48 | tt = time.time() 49 | with tf.Session() as sess: 50 | sess.run(init_op) 51 | for i in range(100): 52 | try: 53 | images, filenames, labels = iterator.get_next() 54 | print('{} -> {}'.format(i, sess.run(labels))) 55 | except tf.errors.OutOfRangeError: 56 | sess.run(init_op) 57 | print(time.time() - tt) 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # yueye 2 | Code of my blog: yueye 3 | 4 | 1. [XOR with TensorFlow.](./xor_with_tensorflow.py) 5 | 6 | [Blog link.](https://www.yueye.org/2017/xor-with-tensorflow.html) 7 | 8 | 2. [FizzBuzz with TensorFlow.](./fizzbuzz_with_tensorflow.py) 9 | 10 | [Blog link.](https://www.yueye.org/2017/fizzbuzz-with-tensorflow.html) 11 | 12 | 3. [Plot Mask on Image.](./plot_mask_on_image.py) 13 | 14 | [Blog link.](https://www.yueye.org/2017/plot-mask-on-image.html) 15 | 16 | 4. [Visualize outputs of intermediate layers of a Keras model. (Method 1)](./visualize_outputs_keras_model1.py) 17 | 18 | [Visualize outputs of intermediate layers of a Keras model. (Method 2)](./visualize_outputs_keras_model2.py) 19 | 20 | [Blog link.](https://www.yueye.org/2017/visualize-the-output-of-intermediate-layers-of-a-keras-model.html) 21 | 22 | 5. [TensorFlow Dataset with pre-reading image data in numpy array.](./tf_dataset_samples/ds_numpy.py) 23 | 24 | [TensorFlow Dataset with generator.](./tf_dataset_samples/ds_generator.py) 25 | 26 | [TensorFlow Dataset with tensor operations.](./tf_dataset_samples/ds_tensor.py) 27 | 28 | [Blog link.](https://www.yueye.org/2018/memo-of-tensorflow-dataset.html) 29 | 30 | 6. [Convert custom own dataset to pascal_voc format.](./convert_to_pascal_voc.py) 31 | 32 | [Blog link.](https://www.yueye.org/2018/convert-your-own-custom-dataset-to-pascal-voc.html) 33 | 34 | 7. [Plot Chinese characters on images. (using Matplotlib)](./plot_chinese_matplotlib.py) 35 | 36 | [Plot Chinese characters on images. (using PIL.Image)](./plot_chinese_PIL_ImageDraw.py) 37 | 38 | [Plot Chinese characters on images. (using cv2)](./plot_chinese_cv2.py) 39 | 40 | [Blog link.](https://www.yueye.org/2018/plot-chinese-characters-on-images.html) 41 | 42 | 8. [Convert dataset to COCO json format to train models using Detectron](./convert_to_detectron_json.py) 43 | 44 | [Blog link.](https://www.yueye.org/2018/train-object-detection-model-using-detectron.html) -------------------------------------------------------------------------------- /fizzbuzz_with_tensorflow.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import numpy as np 4 | import tensorflow as tf 5 | 6 | DIGITS = 20 7 | 8 | 9 | def binary_encode(num, digits=DIGITS): 10 | return [num >> i & 1 for i in range(digits)][::-1] 11 | 12 | 13 | def label_encode(num): 14 | if num % 15 == 0: 15 | return [1, 0, 0, 0] 16 | elif num % 3 == 0: 17 | return [0, 1, 0, 0] 18 | elif num % 5 == 0: 19 | return [0, 0, 1, 0] 20 | else: 21 | return [0, 0, 0, 1] 22 | 23 | 24 | def get_data(num, low=101, high=10000): 25 | binary_num_list = [] 26 | label_list = [] 27 | for i in range(num): 28 | n = np.random.randint(low, high, 1)[0] 29 | binary_num_list.append(np.array(binary_encode(n))) 30 | label_list.append(np.array(label_encode(n))) 31 | return np.array(binary_num_list), np.array(label_list) 32 | 33 | 34 | def model(data): 35 | with tf.variable_scope('layer1') as scope: 36 | weight = tf.get_variable('weight', shape=(DIGITS, 256)) 37 | bias = tf.get_variable('bias', shape=(256,)) 38 | x = tf.nn.relu(tf.matmul(data, weight) + bias) 39 | 40 | with tf.variable_scope('layer2') as scope: 41 | weight = tf.get_variable('weight', shape=(256, 4)) 42 | bias = tf.get_variable('bias', shape=(4,)) 43 | x = tf.matmul(x, weight) + bias 44 | 45 | return x 46 | 47 | 48 | def main(): 49 | data = tf.placeholder(tf.float32, shape=(None, DIGITS)) 50 | label = tf.placeholder(tf.float32, shape=(None, 4)) 51 | 52 | x = model(data) 53 | preds = tf.argmax(tf.nn.softmax(x), 1) 54 | acc = tf.reduce_mean(tf.cast(tf.equal(preds, tf.argmax(label, 1)), tf.float32)) 55 | loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=label, logits=x)) 56 | optimizer = tf.train.AdamOptimizer(0.01).minimize(loss) 57 | 58 | with tf.Session() as sess: 59 | sess.run(tf.global_variables_initializer()) 60 | for step in range(3000): 61 | train_data, train_label = get_data(128) 62 | _, a = sess.run([optimizer, acc], 63 | feed_dict={data: train_data, label: train_label}) 64 | if step % 300 == 0: 65 | print('Step: {} -> Accuracy: {:.3f}'.format(step, a)) 66 | 67 | test_data = np.array([binary_encode(i) for i in range(1, 101)]) 68 | pred = sess.run(preds, feed_dict={data: test_data}) 69 | results = [] 70 | for i in range(1, 101): 71 | results.append('{}'.format(['fizzbuzz', 'fizz', 'buzz', i][pred[i - 1]])) 72 | print(', '.join(results)) 73 | 74 | 75 | if __name__ == '__main__': 76 | main() 77 | -------------------------------------------------------------------------------- /plot_mask_on_image.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import cv2 4 | import numpy as np 5 | from skimage import measure 6 | 7 | 8 | def write(image, path='test.png'): 9 | cv2.imwrite(path, image) 10 | 11 | 12 | def draw_mask_edge_on_image_cv2(image, mask, color=(0, 0, 255)): 13 | coef = 255 if np.max(image) < 3 else 1 14 | image = (image * coef).astype(np.float32) 15 | contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 16 | image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) 17 | cv2.drawContours(image, contours, -1, color, 1) 18 | write(image) 19 | 20 | 21 | def draw_mask_on_image(image, mask, color=(0, 0, 255)): 22 | coef = 255 if np.max(image) < 3 else 1 23 | image = (image * coef).astype(np.float32) 24 | contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 25 | image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) 26 | cv2.drawContours(image, contours, -1, color, -1) 27 | write(image) 28 | 29 | 30 | def draw_mask_edge_on_image_skimage(image, mask, color=(0, 0, 255)): 31 | coef = 255 if np.max(image) < 3 else 1 32 | image = (image * coef).astype(np.float32) 33 | contours = measure.find_contours(mask, 0.5) 34 | image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) 35 | for c in contours: 36 | c = np.around(c).astype(np.int) 37 | image[c[:, 0], c[:, 1]] = np.array(color) 38 | write(image) 39 | 40 | 41 | def draw_mask_edge_on_skimage_using_cv2(image, mask, color=(0, 0, 255)): 42 | coef = 255 if np.max(image) < 3 else 1 43 | image = (image * coef).astype(np.float32) 44 | contours = measure.find_contours(mask, 0.5) 45 | image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) 46 | _contours = [] 47 | for c in contours: 48 | c[:, [0, 1]] = c[:, [1, 0]] 49 | _contours.append(np.around(np.expand_dims(c, 1)).astype(np.int)) 50 | cv2.drawContours(image, _contours, -1, color, 1) 51 | write(image) 52 | 53 | 54 | def draw_mask_on_image_cv2(image, mask): 55 | coef = 255 if np.max(image) < 3 else 1 56 | image = (image * coef).astype(np.float32) 57 | image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) 58 | image[..., 2] = np.where(mask == 255, 255, image[..., 2]) 59 | write(image) 60 | 61 | 62 | def main(): 63 | image = cv2.imread('image.png', 0) 64 | mask = cv2.imread('mask.png', 0) 65 | 66 | write(image) 67 | write(mask) 68 | 69 | mask = np.where(mask > 150, 255, np.where(mask < 100, 0, mask)) 70 | 71 | draw_mask_on_image_cv2(image, mask) 72 | 73 | draw_mask_edge_on_image_cv2(image, mask) 74 | 75 | draw_mask_edge_on_image_skimage(image, mask) 76 | 77 | draw_mask_edge_on_skimage_using_cv2(image, mask) 78 | 79 | draw_mask_on_image(image, mask) 80 | 81 | 82 | if __name__ == '__main__': 83 | main() 84 | -------------------------------------------------------------------------------- /convert_to_detectron_json.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import json 4 | import os 5 | import sys 6 | 7 | import cv2 8 | 9 | if len(sys.argv) < 3: 10 | print "Usage: python convert_to_detectron_json.py root_path phase split" 11 | print "For example: python convert_to_detectron_json.py data train 100200" 12 | exit(1) 13 | 14 | root_path = sys.argv[1] 15 | phase = sys.argv[2] 16 | split = int(sys.argv[3]) 17 | 18 | dataset = { 19 | 'licenses': [], 20 | 'info': {}, 21 | 'categories': [], 22 | 'images': [], 23 | 'annotations': [] 24 | } 25 | 26 | with open(os.path.join(root_path, 'classes.txt')) as f: 27 | classes = f.read().strip().split() 28 | 29 | for i, cls in enumerate(classes, 1): 30 | dataset['categories'].append({ 31 | 'id': i, 32 | 'name': cls, 33 | 'supercategory': 'beverage' 34 | }) 35 | 36 | 37 | def get_category_id(cls): 38 | for category in dataset['categories']: 39 | if category['name'] == cls: 40 | return category['id'] 41 | 42 | 43 | _indexes = sorted([f.split('.')[0] for f in os.listdir(os.path.join(root_path, 'annos'))]) 44 | if phase == 'train': 45 | indexes = [line for line in _indexes if int(line) > split] 46 | else: 47 | indexes = [line for line in _indexes if int(line) <= split] 48 | 49 | j = 1 50 | for index in indexes: 51 | im = cv2.imread(os.path.join(root_path, 'images/') + index + '.jpg') 52 | height, width, _ = im.shape 53 | dataset['images'].append({ 54 | 'coco_url': '', 55 | 'date_captured': '', 56 | 'file_name': index + '.jpg', 57 | 'flickr_url': '', 58 | 'id': int(index), 59 | 'license': 0, 60 | 'width': width, 61 | 'height': height 62 | }) 63 | 64 | anno_file = os.path.join(root_path, 'annos/') + index + '.txt' 65 | with open(anno_file) as f: 66 | lines = [line for line in f.readlines() if line.strip()] 67 | 68 | for i, line in enumerate(lines): 69 | parts = line.strip().split() 70 | cls = parts[0] 71 | x1 = int(parts[1]) 72 | y1 = int(parts[2]) 73 | x2 = int(parts[3]) 74 | y2 = int(parts[4]) 75 | width = max(0, x2 - x1) 76 | height = max(0, y2 - y1) 77 | dataset['annotations'].append({ 78 | 'area': width * height, 79 | 'bbox': [x1, y1, width, height], 80 | 'category_id': get_category_id(cls), 81 | 'id': j, 82 | 'image_id': int(index), 83 | 'iscrowd': 0, 84 | 'segmentation': [] 85 | }) 86 | j += 1 87 | 88 | folder = os.path.join(root_path, 'annotations') 89 | if not os.path.exists(folder): 90 | os.makedirs(folder) 91 | json_name = os.path.join(root_path, 'annotations/{}.json'.format(phase)) 92 | with open(json_name, 'w') as f: 93 | json.dump(dataset, f) 94 | -------------------------------------------------------------------------------- /visualize_outputs_keras_model2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | from __future__ import absolute_import, division, print_function 4 | 5 | import keras 6 | import keras.backend as K 7 | import matplotlib.pyplot as plt 8 | import numpy as np 9 | 10 | 11 | def get_image(): 12 | img_size = 64 13 | min_rect_size = 5 14 | max_rect_size = 50 15 | img = np.zeros((img_size, img_size, 1)) 16 | num = np.random.choice(range(3)) 17 | if num == 0: # equal 18 | xsize = np.random.randint(min_rect_size, max_rect_size) 19 | ysize = xsize 20 | print('Shape: {}'.format('equal')) 21 | elif num == 1: # width 22 | ysize = np.random.randint(max_rect_size / 2, max_rect_size) 23 | ratio = np.random.choice([1.5, 2, 3]) 24 | xsize = int(ysize / ratio) 25 | print('Shape: {}'.format('width')) 26 | elif num == 2: # long 27 | xsize = np.random.randint(max_rect_size / 2, max_rect_size) 28 | ratio = np.random.choice([1.5, 2, 3]) 29 | ysize = int(xsize / ratio) 30 | print('Shape: {}'.format('long')) 31 | 32 | x = np.random.randint(0, img_size - xsize) 33 | y = np.random.randint(0, img_size - ysize) 34 | img[x:x + xsize, y:y + ysize, 0] = 1. 35 | return img 36 | 37 | 38 | model_path = 'test.h5' 39 | model = keras.models.load_model(model_path) 40 | print('Using {}'.format(model_path)) 41 | 42 | 43 | def get_layer_outputs(): 44 | img_to_visualize = get_image() 45 | plt.imshow(img_to_visualize[..., 0]) 46 | plt.show() 47 | img_to_visualize = np.expand_dims(img_to_visualize, 0) 48 | outputs = [layer.output for layer in model.layers] # all layer outputs 49 | comp_graph = [K.function([model.input] + [K.learning_phase()], [output]) 50 | for output in outputs] # evaluation functions 51 | 52 | # Testing 53 | layer_outputs_list = [op([img_to_visualize, 1.]) for op in comp_graph] 54 | layer_outputs = [] 55 | 56 | for layer_output in layer_outputs_list: 57 | print(layer_output[0][0].shape, end='\n-------------------\n') 58 | layer_outputs.append(layer_output[0][0]) 59 | 60 | return layer_outputs 61 | 62 | 63 | def plot_layer_outputs(layer_number): 64 | layer_outputs = get_layer_outputs() 65 | 66 | x_max = layer_outputs[layer_number].shape[0] 67 | y_max = layer_outputs[layer_number].shape[1] 68 | n = layer_outputs[layer_number].shape[2] 69 | 70 | L = [] 71 | for i in range(n): 72 | L.append(np.zeros((x_max, y_max))) 73 | 74 | for i in range(n): 75 | for x in range(x_max): 76 | for y in range(y_max): 77 | L[i][x][y] = layer_outputs[layer_number][x][y][i] 78 | 79 | fig = plt.figure() 80 | for i, c in enumerate(L): 81 | ax = fig.add_subplot(np.ceil(n**0.5), np.ceil(n**0.5), i + 1) 82 | ax.imshow(c, cmap='gray') 83 | plt.show() 84 | 85 | 86 | plot_layer_outputs(3) 87 | -------------------------------------------------------------------------------- /visualize_outputs_keras_model1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | from __future__ import absolute_import, division, print_function 4 | 5 | import keras.backend as K 6 | import matplotlib.pyplot as plt 7 | import numpy as np 8 | from keras.layers import Activation, Conv2D, Dense, Dropout, Flatten, MaxPool2D 9 | from keras.models import Sequential 10 | 11 | model = Sequential() 12 | model.add(Conv2D(64, kernel_size=(3, 3), padding='same', input_shape=(64, 64, 1))) 13 | conv1out = Activation('relu') 14 | model.add(conv1out) 15 | maxpool1out = MaxPool2D() 16 | model.add(maxpool1out) 17 | model.add(Conv2D(128, kernel_size=(3, 3), padding='same')) 18 | conv2out = Activation('relu') 19 | model.add(conv2out) 20 | model.add(MaxPool2D()) 21 | model.add(Conv2D(256, kernel_size=(3, 3), padding='same')) 22 | conv3out = Activation('relu') 23 | model.add(conv3out) 24 | model.add(MaxPool2D()) 25 | model.add(Conv2D(512, kernel_size=(3, 3), padding='same')) 26 | conv4out = Activation('relu') 27 | model.add(conv4out) 28 | model.add(MaxPool2D()) 29 | model.add(Flatten()) 30 | model.add(Dense(64, activation='relu')) 31 | model.add(Dropout(0.6)) 32 | model.add(Dense(3, activation='softmax')) 33 | 34 | model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) 35 | 36 | model.load_weights('test.h5') 37 | 38 | 39 | def get_image(): 40 | img_size = 64 41 | min_rect_size = 5 42 | max_rect_size = 50 43 | img = np.zeros((img_size, img_size, 1)) 44 | num = np.random.choice(range(3)) 45 | if num == 0: # equal 46 | xsize = np.random.randint(min_rect_size, max_rect_size) 47 | ysize = xsize 48 | print('Shape: {}'.format('equal')) 49 | elif num == 1: # width 50 | ysize = np.random.randint(max_rect_size / 2, max_rect_size) 51 | ratio = np.random.choice([1.5, 2, 3]) 52 | xsize = int(ysize / ratio) 53 | print('Shape: {}'.format('width')) 54 | elif num == 2: # long 55 | xsize = np.random.randint(max_rect_size / 2, max_rect_size) 56 | ratio = np.random.choice([1.5, 2, 3]) 57 | ysize = int(xsize / ratio) 58 | print('Shape: {}'.format('long')) 59 | 60 | x = np.random.randint(0, img_size - xsize) 61 | y = np.random.randint(0, img_size - ysize) 62 | img[x:x + xsize, y:y + ysize, 0] = 1. 63 | return img 64 | 65 | 66 | img_to_visualize = get_image() 67 | plt.imshow(img_to_visualize[..., 0]) 68 | plt.show() 69 | 70 | img_to_visualize = np.expand_dims(img_to_visualize, 0) 71 | 72 | 73 | def layer_to_visualize(layer): 74 | inputs = [K.learning_phase()] + model.inputs 75 | 76 | _convout1_f = K.function(inputs, [layer.output]) 77 | 78 | def convout1_f(X): 79 | # The [0] is to disable the training phase flag 80 | return _convout1_f([0] + [X]) 81 | 82 | convolutions = convout1_f(img_to_visualize) 83 | convolutions = np.squeeze(convolutions) 84 | 85 | print ('Shape of conv:', convolutions.shape) 86 | 87 | num = convolutions.shape[2] 88 | n = int(np.ceil(np.sqrt(num))) 89 | 90 | # Visualization of each filter of the layer 91 | fig = plt.figure() 92 | for i in range(num): 93 | ax = fig.add_subplot(n, n, i + 1) 94 | ax.imshow(convolutions[..., i], cmap='gray') 95 | plt.show() 96 | fig.close() 97 | 98 | 99 | # Specify the layer to want to visualize 100 | layer_to_visualize(conv1out) 101 | # layer_to_visualize(conv2out) 102 | # layer_to_visualize(conv3out) 103 | # layer_to_visualize(conv4out) 104 | --------------------------------------------------------------------------------