From c9f5b25caec1b11990bf816a6a21513bd2fb76c7 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Mon, 30 Apr 2018 18:23:58 +0200 Subject: [PATCH 01/28] Add files via upload --- Work/CNN_XRAY.py | 227 ++++++++++++++++++ Work/NN-Xray.py | 112 +++++++++ Work/numpyrec/NN.py | 100 ++++++++ Work/numpyrec/nprecord.py | 64 +++++ Work/tfRecord/Tfrecord.py | 199 +++++++++++++++ Work/tfRecord/__init__.py | 2 + .../__pycache__/Tfrecord.cpython-36.pyc | Bin 0 -> 3599 bytes .../__pycache__/__init__.cpython-36.pyc | Bin 0 -> 205 bytes .../decode_tfrecord.cpython-36.pyc | Bin 0 -> 3051 bytes Work/tfRecord/decode_tfrecord.py | 128 ++++++++++ Work/utils_data/norma/norma.py | 7 + 11 files changed, 839 insertions(+) create mode 100644 Work/CNN_XRAY.py create mode 100644 Work/NN-Xray.py create mode 100644 Work/numpyrec/NN.py create mode 100644 Work/numpyrec/nprecord.py create mode 100644 Work/tfRecord/Tfrecord.py create mode 100644 Work/tfRecord/__init__.py create mode 100644 Work/tfRecord/__pycache__/Tfrecord.cpython-36.pyc create mode 100644 Work/tfRecord/__pycache__/__init__.cpython-36.pyc create mode 100644 Work/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc create mode 100644 Work/tfRecord/decode_tfrecord.py create mode 100644 Work/utils_data/norma/norma.py diff --git a/Work/CNN_XRAY.py b/Work/CNN_XRAY.py new file mode 100644 index 0000000..90da2f1 --- /dev/null +++ b/Work/CNN_XRAY.py @@ -0,0 +1,227 @@ +import tensorflow as tf +import tfRecord as trec + + +T_HEIGHT = 32 +T_WIDTH = 32 +T_channels = 3 + +TRAIN_FILE = 'tftrain.tfrecords' +TEST_FILE = 'tftest.tfrecords' + +learning_rate = 0.001 + +num_steps = 1500 + +batch_size = 8 + +display_step = 10 + + + +# Network Parameters + +num_input = T_HEIGHT * T_WIDTH * T_channels + +num_classes = 2 + + + + +# tf Graph input + +X = tf.placeholder(tf.float32, [None, num_input]) + +Y = tf.placeholder(tf.int64, [None, num_classes]) + + + + + + + +# Create some wrappers for simplicity + +def conv2d(x, W, b, strides=1): + + # Conv2D wrapper, with bias and relu activation + + x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') + + x = tf.nn.bias_add(x, b) + + return tf.nn.relu(x) + + + + + +def maxpool2d(x, k=2): + + # MaxPool2D wrapper + + return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], + + padding='SAME') + + + + + +# Create model + +def conv_net(x, weights, biases): + + + x = tf.reshape(x, shape=[-1, T_HEIGHT, T_WIDTH, T_channels]) + + + + # Convolution Layer + + conv1 = conv2d(x, weights['wc1'], biases['bc1']) + + # Max Pooling (down-sampling) + + conv1 = maxpool2d(conv1, k=2) + + + + # Convolution Layer + + conv2 = conv2d(conv1, weights['wc2'], biases['bc2']) + + # Max Pooling (down-sampling) + + conv2 = maxpool2d(conv2, k=2) + + + + + # Fully connected layer + + # Reshape conv2 output to fit fully connected layer input + + fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]]) + + fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) + + fc1 = tf.nn.relu(fc1) + + # Output, class prediction + + out = tf.add(tf.matmul(fc1, weights['out']), biases['out']) + + return out + + + +# Store layers weight & bias + +weights = { + + # 5x5 conv, 1 input, 32 outputs + + 'wc1': tf.Variable(tf.random_normal([3, 3, 3, 32])), + + # 5x5 conv, 32 inputs, 64 outputs + + 'wc2': tf.Variable(tf.random_normal([3, 3, 32, 64])), + + # fully connected, 32*32*64 inputs, 1024 outputs + + 'wd1': tf.Variable(tf.random_normal([8*8*64, 512])), + + # 1024 inputs, 10 outputs (class prediction) + + 'out': tf.Variable(tf.random_normal([512, num_classes])) + +} + + + +biases = { + + 'bc1': tf.Variable(tf.random_normal([32])), + + 'bc2': tf.Variable(tf.random_normal([64])), + + 'bd1': tf.Variable(tf.random_normal([512])), + + 'out': tf.Variable(tf.random_normal([num_classes])) + +} + + + +# Construct model + +logits = conv_net(X, weights, biases) + +prediction = tf.nn.softmax(logits) + + + +# Define loss and optimizer + +loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( + logits=logits, labels=Y)) + +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) + +train_op = optimizer.minimize(loss_op) + + + + + +# Evaluate model + +correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1)) + +accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.int64)) + + + +# Initialize the variables (i.e. assign their default value) +tfrec_dev_input_fn = trec.decode_tfrecord.tfrec_data_input_fn(["catvdog.tfrecords"],batch_size=batch_size) +features, labels = tfrec_dev_input_fn() + +init = tf.global_variables_initializer() + + + +# Start training + +with tf.Session() as sess: + + + + # Run the initializer + + sess.run(init) + + + + for step in range(1, num_steps+1): + img, label = sess.run([features['image'], labels]) + + # Run optimization op (backprop) + sess.run(train_op, feed_dict={X: img, Y: label}) + + if step % display_step == 0 or step == 1: + # Calculate batch loss and accuracy + loss, acc = sess.run([loss_op, accuracy], feed_dict={X: img, + Y: label}) + print("Step " + str(step) + ", Minibatch Loss= " + \ + "{:.4f}".format(loss) + ", Training Accuracy= " + \ + "{:.3f}".format(acc)) + + print("Optimization Finished!") + '''tfrec_dev_input_fn = trec.decode_tfrecord.tfrec_data_input_fn(["catvdogtest.tfrecords"], batch_size=1000) + features, labels = tfrec_dev_input_fn() + img, label = sess.run([features['image'], labels]) + print("Testing Accuracy:", \ + sess.run(accuracy, feed_dict={X: img, + Y: label}))''' + sess.close() \ No newline at end of file diff --git a/Work/NN-Xray.py b/Work/NN-Xray.py new file mode 100644 index 0000000..40788bf --- /dev/null +++ b/Work/NN-Xray.py @@ -0,0 +1,112 @@ +import tensorflow as tf +import numpy as np +import tfRecord as trec +T_HEIGHT = 256 +T_WIDTH = 256 + +TRAIN_FILE = 'tftrain.tfrecords' +TEST_FILE = 'tftest.tfrecords' + +learning_rate = 0.001 +num_steps = 1500 +batch_size = 16 +display_step = 10 + +n_hidden_1 = 3 # 1st layer number of neurons +n_hidden_2 = 3 # 2nd layer number of neurons +num_input = 3072 # data input (img shape: 256*256) +num_classes = 2 # + +# tf Graph input +X = tf.placeholder("float", [None, num_input]) +Y = tf.placeholder(tf.int64, [None, num_classes]) + +# Store layers weight & bias +weights = { + 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), + 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), + 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) +} +biases = { + 'b1': tf.Variable(tf.random_normal([n_hidden_1])), + 'b2': tf.Variable(tf.random_normal([n_hidden_2])), + 'out': tf.Variable(tf.random_normal([num_classes])) +} + + +# Create model +def neural_net(x): + # Hidden fully connected layer with 256 neurons + layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) + z1 = tf.nn.relu(layer_1) + # Hidden fully connected layer with 256 neurons + layer_2 = tf.add(tf.matmul(z1, weights['h2']), biases['b2']) + # Output fully connected layer with a neuron for each class + '''z2 = tf.nn.relu(layer_2)''' + out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + return out_layer + +# Construct model +logits = neural_net(X) +prediction = tf.nn.softmax(logits) + +# Define loss and optimizer +loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2( + labels=Y, logits=logits)) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) +train_op = optimizer.minimize(loss_op) + +# Evaluate model +correct_pred = tf.equal(tf.argmax(prediction,1), tf.argmax(Y, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) +#### + + + + + +tfrec_dev_input_fn = trec.decode_tfrecord.tfrec_data_input_fn(["catvdog.tfrecords"]) +features, labels = tfrec_dev_input_fn() + +# Initialize the variables (i.e. assign their default value) +init = tf.global_variables_initializer() + +# Start training +with tf.Session() as sess: + + # Run the initializer + sess.run(init) + + for step in range(1, num_steps+1): + img, label = sess.run([features['image'], labels]) + + # Run optimization op (backprop) + sess.run(train_op, feed_dict={X: img, Y: label}) + + if step % display_step == 0 or step == 1: + # Calculate batch loss and accuracy + loss, acc = sess.run([loss_op, accuracy], feed_dict={X: img, + Y: label}) + print("Step " + str(step) + ", Minibatch Loss= " + \ + "{:.4f}".format(loss) + ", Training Accuracy= " + \ + "{:.3f}".format(acc)) + + print("Optimization Finished!") + tfrec_dev_input_fn = trec.decode_tfrecord.tfrec_data_input_fn(["catvdogtest.tfrecords"], batch_size=1000) + features, labels = tfrec_dev_input_fn() + img, label = sess.run([features['image'], labels]) + print("Testing Accuracy:", \ + sess.run(accuracy, feed_dict={X: img, + Y: label})) + '''tfrec_dcatvdog= trec.decode_tfrecord.tfrec_data_catvdog(["catvdogtest2.tfrecords"]) + a = tfrec_dcatvdog() + for i in range(750): + img = sess.run([a['image']]) + print("prediction :", \ + sess.run(prediction, feed_dict={X:img})) + print(sess.run([weights['h1'], biases['b1'], weights['h2'], biases['b2']])) + print(sess.run([weights['out'],biases['out']]))''' + + + + \ No newline at end of file diff --git a/Work/numpyrec/NN.py b/Work/numpyrec/NN.py new file mode 100644 index 0000000..302af5d --- /dev/null +++ b/Work/numpyrec/NN.py @@ -0,0 +1,100 @@ +import tensorflow as tf +import numpy as np +T_HEIGHT = 256 +T_WIDTH = 256 + +TRAIN_FILE = 'tftrain.tfrecords' +TEST_FILE = 'tftest.tfrecords' + +learning_rate = 0.001 +num_steps = 1500 +batch_size = 16 +display_step = 10 + +n_hidden_1 = 3 # 1st layer number of neurons +n_hidden_2 = 3 # 2nd layer number of neurons +num_input = 49152 # data input (img shape: 256*256) +num_classes = 2 # + +# tf Graph input +X = tf.placeholder("float", [None, num_input]) +Y = tf.placeholder(tf.int64, [None, num_classes]) + +# Store layers weight & bias +weights = { + 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), + 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), + 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) +} +biases = { + 'b1': tf.Variable(tf.random_normal([n_hidden_1])), + 'b2': tf.Variable(tf.random_normal([n_hidden_2])), + 'out': tf.Variable(tf.random_normal([num_classes])) +} + + +# Create model +def neural_net(x): + # Hidden fully connected layer with 256 neurons + layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) + z1 = tf.nn.relu(layer_1) + # Hidden fully connected layer with 256 neurons + layer_2 = tf.add(tf.matmul(z1, weights['h2']), biases['b2']) + # Output fully connected layer with a neuron for each class + '''z2 = tf.nn.relu(layer_2)''' + out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + return out_layer + +# Construct model +logits = neural_net(X) +prediction = tf.nn.softmax(logits) + +# Define loss and optimizer +loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2( + labels=Y, logits=logits)) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) +train_op = optimizer.minimize(loss_op) + +# Evaluate model +correct_pred = tf.equal(prediction, tf.argmax(Y, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) +#### + + +# Initialize the variables (i.e. assign their default value) +init = tf.global_variables_initializer() + +# Start training +with tf.Session() as sess: + + # Run the initializer + sess.run(init) + + for step in range(1, num_steps+1): + img, label = sess.run([features['image'], labels]) + + # Run optimization op (backprop) + sess.run(train_op, feed_dict={X: img, Y: label}) + + if step % display_step == 0 or step == 1: + # Calculate batch loss and accuracy + loss, acc = sess.run([loss_op, accuracy], feed_dict={X: img, + Y: label}) + print("Step " + str(step) + ", Minibatch Loss= " + \ + "{:.4f}".format(loss) + ", Training Accuracy= " + \ + "{:.3f}".format(acc)) + + print("Optimization Finished!") + print("Testing Accuracy:", \ + sess.run(accuracy, feed_dict={X: img, + Y: label})) + for i in range(750): + img = sess.run([a['image']]) + print("prediction :", \ + sess.run(prediction, feed_dict={X:img})) + print(sess.run([weights['h1'], biases['b1'], weights['h2'], biases['b2']])) + print(sess.run([weights['out'],biases['out']])) + + + + \ No newline at end of file diff --git a/Work/numpyrec/nprecord.py b/Work/numpyrec/nprecord.py new file mode 100644 index 0000000..fe99af1 --- /dev/null +++ b/Work/numpyrec/nprecord.py @@ -0,0 +1,64 @@ +import os +import numpy as np +from glob import glob +import cv2 as cv +############### +# Cat vs Dog # +############### + +IMAGE_CHANNELS = 3 +IMAGE_HEIGHT = 128 +IMAGE_WIDTH = 128 +NUM_CLASS = 2 +gen_file_path = "train/train/" + +def get_paths(ur_path): + file_class1 = os.path.join(gen_file_path,'cat*.jpg' ) + file_class2 = os.path.join(gen_file_path, 'dog*.jpg') + cat_files = glob(file_class1) + dog_file = glob(file_class2) + size = len(cat_files) + len(dog_file) + return cat_files, dog_file, size + +def get_numpy(class1_files, class2_files, size): + dataX = np.zeros((size,IMAGE_HEIGHT, IMAGE_WIDTH,IMAGE_CHANNELS), dtype=np.float64) + dataY = np.zeros((size)) + i = 0 + for file in class1_files: + img = cv.imread(file) + img = cv.resize(img,(IMAGE_HEIGHT, IMAGE_WIDTH)) + img_np = np.array(img) + dataX[i] = img_np + dataY[i]= 0 + i += 1 + for file in class2_files: + try: + img = cv.imread(file) + img = cv.resize(img, (IMAGE_HEIGHT, IMAGE_WIDTH)) + img_np = np.array(img) + dataX[i] = img_np + dataY[i] = 1 + i +=1 + except: + continue + return dataX, dataY + +def make_npfile(dir, inputs, labels): + ###### + #Space dedicated for pre-processing methods + ###### + if (dir == ""): + dir = os.getcwd() + np.save(dir, [inputs, labels], ["inputs, labels"]) + return 0 + +def load_npfile(filename): + f = os.path.join(os.path.dirname(filename), filename) + data = np.load(f) + features = data[0] + labels = data[1] + return features, labels +alpha, beta, gamma = get_paths(gen_file_path) +a , b = get_numpy(alpha, beta, gamma) +make_npfile("", a, b) +'''a, b= load_npfile("train.npy")''' diff --git a/Work/tfRecord/Tfrecord.py b/Work/tfRecord/Tfrecord.py new file mode 100644 index 0000000..3c8fd3d --- /dev/null +++ b/Work/tfRecord/Tfrecord.py @@ -0,0 +1,199 @@ +from random import shuffle, seed + +import glob + +import numpy as np + +import cv2 + +import tensorflow as tf + +import sys + +import matplotlib.pyplot as plt + + + +UR_DATA_PATH = "" + +shuffle_data = True + + + +# This function returns the different paths and corresponding labels + +def read_paths(ur_path): + + #Reading the path of each data(ex: image) and then extracting the labels from path + + data_path = glob.glob(ur_path) + + labels = [int(i.split('.')[0][-1]) for i in data_path] + + return data_path, labels + + +def shuffling_data(data_path, labels): + + c = list(zip(data_path, labels)) + + shuffle(c) + + #NOTE: data and labeld are tuples. Care about immutability + + data, labeled = zip(*c) #NOTE:ith element in data correpand to ith label elmt + + return data, labeled + + +def split_data(training_prop,test_prop, data, labels, dev_prop=0): + + ''' + + - training_prop is required .numerical float < 1 + + - dev_prop is optional. Numerical float < 0.3 + + - test_prop is required. Numerical float < 0.3 + + - data and labels are lists + + ''' + + #NOTE: data is a list of paths of each image + + training_data = data[0:int(training_prop*len(data))] + + training_labels = labels[0:int(training_prop*len(labels))] + + + + if dev_prop != 0: + + dev_data = data[int(training_prop*len(data)):int((1-dev_prop)*len(data))] + + dev_labels = labels[int(training_prop*len(labels)):int((1-dev_prop)*len(labels))] + + test_data = data[int((1-dev_prop)*len(data)):] + + test_labels = labels[int((1-dev_prop)*len(labels)):] + + return training_data,training_labels, dev_data, dev_labels, test_data, test_labels + + else: + + test_data = data[int((1-test_prop)*len(data)):] + + test_labels = labels[int((1-test_prop)*len(labels)):] + + return training_data, training_labels, test_data, test_labels + + + +# Load image + +def load_image(data, s_width, s_height): + + #cv2 load data from data (value of one path) + img_load = cv2.imread(data) + img_after = cv2.resize(img_load, dsize=(s_width, s_height)) + + img_after = cv2.cvtColor(img_after, cv2.COLOR_BGR2GRAY) + img_after = img_after.astype(np.float32) + img_after = img_after.tostring() + return img_after + +a = load_image("pulmonary_chest_xray/ChinaSet_AllFiles/CXR_png/CHNCXR_0001_0.png", 1000, 1000) + + +def _int64_feature(value): + + return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) + + + +def _bytes_feature(value): + + return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) + + + +def tfrecord_file(data, labeled, x_filename, s_width, s_height): + + #NOTE:data is a tuple (list) of paths of each image + + #NOTE: data can come from the dev-set or test-set + + #NOTE: Same for labeled + + + + file_path = x_filename + + #Open a TFRecordWriter + + writer = tf.python_io.TFRecordWriter(file_path) + + for i in range(1): + + new_img = load_image(data[i], s_width, s_height) + + new_label = labeled[i] + + new_feature = {'train/label': _int64_feature(new_label), + + 'train/image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + + + #NOTE: PLease refer to this url for defenirtion: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto + + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + + writer.write(example.SerializeToString()) + + writer.close() + + sys.stdout.flush() + + return 0 + + +data_path, labels = read_paths("pulmonary_chest_xray/ChinaSet_AllFiles/CXR_png/*.png") + +tfrecord_file(data_path, labels, "Tfexamp.tfrecords", 1000, 1000) + +'''data_test_path, test_labels = read_paths("pulmonary_chest_xray/MontgomerySet/CXR_png/*.png") + +tfrecord_file(data_test_path, test_labels, "TfTestSet.tfrecords",1000,1000)''' + + + +def read_file(x_filename, x_capacity=662): + + feature = {'train/image': tf.FixedLenFeature([], tf.string), + + 'train/label': tf.FixedLenFeature([], tf.int64) + + } + + queue = tf.train.string_input_producer([x_filename], num_epochs=1) + + reader = tf.TFRecordReader() + + _, serialized_example = reader.read(queue) + + features = tf.parse_single_example(serialized_example, feature=feature) + + + + #x means training, dev or test + + x_image = tf.decode_raw(features['train/image'], tf.float32) + + x_label = tf.cast(features['train/label'], tf.int64) + + + #NOTE: In this line, we can add a instruction to preprocess the images to their original shape + + return x_image, x_label \ No newline at end of file diff --git a/Work/tfRecord/__init__.py b/Work/tfRecord/__init__.py new file mode 100644 index 0000000..dab8f1b --- /dev/null +++ b/Work/tfRecord/__init__.py @@ -0,0 +1,2 @@ +from . import Tfrecord +from . import decode_tfrecord \ No newline at end of file diff --git a/Work/tfRecord/__pycache__/Tfrecord.cpython-36.pyc b/Work/tfRecord/__pycache__/Tfrecord.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b256d391818616bea6ef6a8b64a54b8e549d78a9 GIT binary patch literal 3599 zcmbVPOOM;g5hj}tQItkAqxECPyGVr1Yqde-vAxJ4F^sjx@ooU`Y%nv6O^8_#w7Mlx zONn%ovS$Vq5J1k+L2}6{e;`19L9PLE&bhBSB>oF~Nxo`I@_05#kWgTEb+N0uy85fC z)}4(F|IfzXgHPIq@i*hb*(^EUdc{4IV3{WY=4uP)3J7F-W@#5HkU?C@(^#(`~b^Bukmjk}y38QphY!G5F5 zR4pIP27^Q>I~Rfzq;JrJv@T~pb^Bz@Agg4#^# zd8*E%BC1F?iJpig*Jf4C2GlD5wbET$i|}LEK5H=vXc>K4n;vqNjaU2neGr9Fm?d1b zVC3O~mFP|4v0ob}Y;Mi%(kgB3b@QNWDVq#c)_I&s(%E(77CO0&MwxwOlclPrjTHQ{ zm8}W{@pKr{yl+D&_s|%y)3cjQ?tt!-jr26|(mu4UJ2&c)cP{IxmDY0JQp<&T7xj9i@3IDNXA!fix@PbQ&eS&cW9N_Ih_#A_btomRT+G zUu5XiJJFPPDin7jDLMqGd}+RpN=b))&}EXKp{ztqyNzJujeQ06_3H8(W@-E(@hx|8 zVK%K8Y_5f?b*q{BlTR{BZlzSS8(M$Gzl>sE=Ln79TUa|M1B|U5wqtwjJ^bFmZ_B1u z-oTeWugVh�Q0;AF%JB2&0VJ^|$!k``} z&knjK3P2v8i*8+6{b#q88&4=1C|3$%2VVbKv7aWX3|jl2KK}GDeDKlX?T-%c|3bM@ zUYun@v1z7i8WMMIE3ZiNLIOYtZQ299yswo;NEpLpnSegZ{&P(@fcx-)+&u5YQamC zl$WM1B1pB)vnZJf5I(a14?hSAjHnN+7$`@BM%5mPsF+Ei>OZ9!k7f+;%Z8%%y5^#5(7UW`n9at2f`O_bjw3*Muqi$>KS(8vF1T_wJ>E{D*{x*q6t z)}?bIr0T5u8xSR%JCz$t2l#VOM+Nfj0yX)IlX@yBEmGD-uT=Ar?oIpjW)+y z<87k2gM)JqbcX1%^M*d6n>tKa1C2O%tG3)INN=@gonE?a?K@S^&Wcew4dYa`jvrO^ z`*Rrs%F2<^bSN}J_0dW4kaDS%M}_jDJk-9_?pL*+oJNxjHyiI!b?~`zo|7K6eS~xy zB?!Xf^hn2)a{5V{3uWbJxpMP@r!(9+2FWZRb(>WtyG~2_r{Ms&pGHX7?}8z}Mcg{^ zme&Bif|OJvgW6O)N2Co^oe1rXph4tv->N1m6E7onqgY;7^lE3&l~V;ad>h5M#kQEo zTz19XVQo-8lZ1)$5B|rHeV@YgeEWEyjp(hG%wPQu7l`%6#E0s22oMnmDN#o>1B7r<0mrS<;uwXTuBUTnPVnw6yTtoE`IUZwM=lUyIBy$nwPy&Q3Pg|k3 z^;0fF89kRBTG;L*<9B^|4}$<0>qlACkBc*Hw{o7t1#(3;IKXyeLT0hq6p$~}^; zomQEhh{_$5HY#}O`)Hk%XjJ_XA9A{b8m!6MHX*6ST5g-Q9eD^Yyhd3Y-9zebV0~N= z(uuxaN+fqfZlX~R9G#s>x=G8oiQyMwnx_(V>v@$7e@xu%NmOJ>S|srk+`)-b?y|SC zqzIay9fl9@AKwpucK`U}pt<~4qKmn{DeEh!uG>2Nl^MOx02=?V@4MRk3n8Ulpm*=x nouqu0hflW_&my#fq~&M5W@izkmUfx#WFx5g&~D8has0Sijfh-W&*OAqL_ee=3oX* zmY0k`C7O)4I6~5jQj_zGQh;p!6d)@lHNFHYn&Ma5v!&?v?wwK%&ZzaSt<8 literal 0 HcmV?d00001 diff --git a/Work/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc b/Work/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..34952f566042af9c4999d1a4f01997fd772b8c41 GIT binary patch literal 3051 zcmbtWTW{1x6!wg->ufGSproaAd!emcSZIK%LJ+D#^Uy*>3l-85WSpJ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~c?2IL%M9cC6I13T7~vZhZzAOf8pvl1@-YF07^KwEFjN&;)=C|1(B zsPR@*VG8)D$@JwT)u8$iUQgKM-ctV-y4DdL*gWez!8{9 zI^zX0n|KEW;@5~olOhSV?+7v)I@e&3Igj16YF7D@CwVI1&*oWy zZ#FOw{EyHiQ#8%hv3?}8UMhR>2K=VrNI7M1W<8+C=GL;J%`J`l8W*%jQ%5=uobs;s bQ?}XTE9g^x90WuSZl8Frf6ia>18?SETyx}{ literal 0 HcmV?d00001 diff --git a/Work/tfRecord/decode_tfrecord.py b/Work/tfRecord/decode_tfrecord.py new file mode 100644 index 0000000..9dd3828 --- /dev/null +++ b/Work/tfRecord/decode_tfrecord.py @@ -0,0 +1,128 @@ +#NOTE: refer to this url: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py +import tensorflow as tf +import numpy as np +import os +import matplotlib.pyplot as plt +import cv2 as cv + + + + + + +def tfrec_data_input_fn(filenames, num_epochs=1, batch_size=16): + + def _input_fn(): + def _parse_record(tf_record): + features = { + 'image': tf.FixedLenFeature([], dtype=tf.string), + 'label': tf.FixedLenFeature([], dtype=tf.int64) + } + record = tf.parse_single_example(tf_record, features) + + image_raw = tf.decode_raw(record['image'], tf.uint8) + label = tf.one_hot(tf.cast(record['label'], tf.int32), depth=2) + + def _normalize(image_x): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image_x, tf.float32) * (1. / 255) - 0.5 + return image + + image_raw = _normalize(image_raw) + '''image_raw = tf.reshape(image_raw, (32,32,3))'''#For vizualizing + return { 'image': image_raw }, label + + # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html + dataset = tf.data.TFRecordDataset(filenames) + dataset = dataset.map(_parse_record) + + dataset = dataset.repeat(num_epochs) + dataset = dataset.batch(batch_size) + + iterator = dataset.make_one_shot_iterator() + features, labels = iterator.get_next() + + return features, labels + + return _input_fn + + +def read_file(x_filename, x_capacity=800): + record_iterator = tf.python_io.tf_record_iterator(path=x_filename) + + example = tf.train.Example() + for str_rec in record_iterator: + example.ParseFromString(str_rec) + height = int(example.features.feature['height'] + .int64_list + .value[0]) + + width = int(example.features.feature['width'] + .int64_list + .value[0]) + + + img_string = (example.features.feature['image'] + .bytes_list + .value[0]) + + label = (example.features.feature['label'].int64_list.value[0]) + + + return 0 + + +def tfrec_data_catvdog(filenames, num_epochs=1, batch_size=16): + + def _input_fn(): + def _parse_record(tf_record): + features = { + 'image': tf.FixedLenFeature([], dtype=tf.string), + } + record = tf.parse_single_example(tf_record, features) + + image_raw = tf.decode_raw(record['image'], tf.uint8) + + + def _normalize(image): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 + return image + + image_n = _normalize(image_raw) + + + + return { 'image': image_n } + + # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html + dataset = tf.data.TFRecordDataset(filenames) + dataset = dataset.map(_parse_record) + + '''dataset = dataset.repeat(num_epochs)''' + '''dataset = dataset.batch(batch_size)''' + + iterator = dataset.make_one_shot_iterator() + features= iterator.get_next() + + return features + + return _input_fn + + + + + +read_file("./catvdogtest.tfrecords") + +'''tfrec_dev_input_fn = tfrec_data_input_fn(["catvdog.tfrecords"]) +features, labels = tfrec_dev_input_fn() + +with tf.Session() as sess: + img, label = sess.run([features['image'], labels]) + img = np.reshape(img, (img.shape[0], -1)) + print(img.shape) + for i in range(16): + plt.imshow(img[i]) + plt.show()''' + \ No newline at end of file diff --git a/Work/utils_data/norma/norma.py b/Work/utils_data/norma/norma.py new file mode 100644 index 0000000..a0c0fb6 --- /dev/null +++ b/Work/utils_data/norma/norma.py @@ -0,0 +1,7 @@ +import tensorflow as tf + + +def normalize(image, label): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 + return image, label \ No newline at end of file From 88438908b5bfe76a334b15da8cce171cece3c639 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:00:04 +0200 Subject: [PATCH 02/28] Delete Tfrecord.py --- Work/tfRecord/Tfrecord.py | 199 -------------------------------------- 1 file changed, 199 deletions(-) delete mode 100644 Work/tfRecord/Tfrecord.py diff --git a/Work/tfRecord/Tfrecord.py b/Work/tfRecord/Tfrecord.py deleted file mode 100644 index 3c8fd3d..0000000 --- a/Work/tfRecord/Tfrecord.py +++ /dev/null @@ -1,199 +0,0 @@ -from random import shuffle, seed - -import glob - -import numpy as np - -import cv2 - -import tensorflow as tf - -import sys - -import matplotlib.pyplot as plt - - - -UR_DATA_PATH = "" - -shuffle_data = True - - - -# This function returns the different paths and corresponding labels - -def read_paths(ur_path): - - #Reading the path of each data(ex: image) and then extracting the labels from path - - data_path = glob.glob(ur_path) - - labels = [int(i.split('.')[0][-1]) for i in data_path] - - return data_path, labels - - -def shuffling_data(data_path, labels): - - c = list(zip(data_path, labels)) - - shuffle(c) - - #NOTE: data and labeld are tuples. Care about immutability - - data, labeled = zip(*c) #NOTE:ith element in data correpand to ith label elmt - - return data, labeled - - -def split_data(training_prop,test_prop, data, labels, dev_prop=0): - - ''' - - - training_prop is required .numerical float < 1 - - - dev_prop is optional. Numerical float < 0.3 - - - test_prop is required. Numerical float < 0.3 - - - data and labels are lists - - ''' - - #NOTE: data is a list of paths of each image - - training_data = data[0:int(training_prop*len(data))] - - training_labels = labels[0:int(training_prop*len(labels))] - - - - if dev_prop != 0: - - dev_data = data[int(training_prop*len(data)):int((1-dev_prop)*len(data))] - - dev_labels = labels[int(training_prop*len(labels)):int((1-dev_prop)*len(labels))] - - test_data = data[int((1-dev_prop)*len(data)):] - - test_labels = labels[int((1-dev_prop)*len(labels)):] - - return training_data,training_labels, dev_data, dev_labels, test_data, test_labels - - else: - - test_data = data[int((1-test_prop)*len(data)):] - - test_labels = labels[int((1-test_prop)*len(labels)):] - - return training_data, training_labels, test_data, test_labels - - - -# Load image - -def load_image(data, s_width, s_height): - - #cv2 load data from data (value of one path) - img_load = cv2.imread(data) - img_after = cv2.resize(img_load, dsize=(s_width, s_height)) - - img_after = cv2.cvtColor(img_after, cv2.COLOR_BGR2GRAY) - img_after = img_after.astype(np.float32) - img_after = img_after.tostring() - return img_after - -a = load_image("pulmonary_chest_xray/ChinaSet_AllFiles/CXR_png/CHNCXR_0001_0.png", 1000, 1000) - - -def _int64_feature(value): - - return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) - - - -def _bytes_feature(value): - - return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) - - - -def tfrecord_file(data, labeled, x_filename, s_width, s_height): - - #NOTE:data is a tuple (list) of paths of each image - - #NOTE: data can come from the dev-set or test-set - - #NOTE: Same for labeled - - - - file_path = x_filename - - #Open a TFRecordWriter - - writer = tf.python_io.TFRecordWriter(file_path) - - for i in range(1): - - new_img = load_image(data[i], s_width, s_height) - - new_label = labeled[i] - - new_feature = {'train/label': _int64_feature(new_label), - - 'train/image': _bytes_feature(tf.compat.as_bytes(new_img)) - } - - - #NOTE: PLease refer to this url for defenirtion: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto - - example = tf.train.Example(features=tf.train.Features(feature=new_feature)) - - writer.write(example.SerializeToString()) - - writer.close() - - sys.stdout.flush() - - return 0 - - -data_path, labels = read_paths("pulmonary_chest_xray/ChinaSet_AllFiles/CXR_png/*.png") - -tfrecord_file(data_path, labels, "Tfexamp.tfrecords", 1000, 1000) - -'''data_test_path, test_labels = read_paths("pulmonary_chest_xray/MontgomerySet/CXR_png/*.png") - -tfrecord_file(data_test_path, test_labels, "TfTestSet.tfrecords",1000,1000)''' - - - -def read_file(x_filename, x_capacity=662): - - feature = {'train/image': tf.FixedLenFeature([], tf.string), - - 'train/label': tf.FixedLenFeature([], tf.int64) - - } - - queue = tf.train.string_input_producer([x_filename], num_epochs=1) - - reader = tf.TFRecordReader() - - _, serialized_example = reader.read(queue) - - features = tf.parse_single_example(serialized_example, feature=feature) - - - - #x means training, dev or test - - x_image = tf.decode_raw(features['train/image'], tf.float32) - - x_label = tf.cast(features['train/label'], tf.int64) - - - #NOTE: In this line, we can add a instruction to preprocess the images to their original shape - - return x_image, x_label \ No newline at end of file From 24f5747bf59c3b860b06ebd635aa5ef8626db9e5 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:00:13 +0200 Subject: [PATCH 03/28] Delete __init__.py --- Work/tfRecord/__init__.py | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Work/tfRecord/__init__.py diff --git a/Work/tfRecord/__init__.py b/Work/tfRecord/__init__.py deleted file mode 100644 index dab8f1b..0000000 --- a/Work/tfRecord/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from . import Tfrecord -from . import decode_tfrecord \ No newline at end of file From 025bd34296176dc5dccb9046734b3cce10b13bf7 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:00:21 +0200 Subject: [PATCH 04/28] Delete decode_tfrecord.py --- Work/tfRecord/decode_tfrecord.py | 128 ------------------------------- 1 file changed, 128 deletions(-) delete mode 100644 Work/tfRecord/decode_tfrecord.py diff --git a/Work/tfRecord/decode_tfrecord.py b/Work/tfRecord/decode_tfrecord.py deleted file mode 100644 index 9dd3828..0000000 --- a/Work/tfRecord/decode_tfrecord.py +++ /dev/null @@ -1,128 +0,0 @@ -#NOTE: refer to this url: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py -import tensorflow as tf -import numpy as np -import os -import matplotlib.pyplot as plt -import cv2 as cv - - - - - - -def tfrec_data_input_fn(filenames, num_epochs=1, batch_size=16): - - def _input_fn(): - def _parse_record(tf_record): - features = { - 'image': tf.FixedLenFeature([], dtype=tf.string), - 'label': tf.FixedLenFeature([], dtype=tf.int64) - } - record = tf.parse_single_example(tf_record, features) - - image_raw = tf.decode_raw(record['image'], tf.uint8) - label = tf.one_hot(tf.cast(record['label'], tf.int32), depth=2) - - def _normalize(image_x): - """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" - image = tf.cast(image_x, tf.float32) * (1. / 255) - 0.5 - return image - - image_raw = _normalize(image_raw) - '''image_raw = tf.reshape(image_raw, (32,32,3))'''#For vizualizing - return { 'image': image_raw }, label - - # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html - dataset = tf.data.TFRecordDataset(filenames) - dataset = dataset.map(_parse_record) - - dataset = dataset.repeat(num_epochs) - dataset = dataset.batch(batch_size) - - iterator = dataset.make_one_shot_iterator() - features, labels = iterator.get_next() - - return features, labels - - return _input_fn - - -def read_file(x_filename, x_capacity=800): - record_iterator = tf.python_io.tf_record_iterator(path=x_filename) - - example = tf.train.Example() - for str_rec in record_iterator: - example.ParseFromString(str_rec) - height = int(example.features.feature['height'] - .int64_list - .value[0]) - - width = int(example.features.feature['width'] - .int64_list - .value[0]) - - - img_string = (example.features.feature['image'] - .bytes_list - .value[0]) - - label = (example.features.feature['label'].int64_list.value[0]) - - - return 0 - - -def tfrec_data_catvdog(filenames, num_epochs=1, batch_size=16): - - def _input_fn(): - def _parse_record(tf_record): - features = { - 'image': tf.FixedLenFeature([], dtype=tf.string), - } - record = tf.parse_single_example(tf_record, features) - - image_raw = tf.decode_raw(record['image'], tf.uint8) - - - def _normalize(image): - """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" - image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 - return image - - image_n = _normalize(image_raw) - - - - return { 'image': image_n } - - # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html - dataset = tf.data.TFRecordDataset(filenames) - dataset = dataset.map(_parse_record) - - '''dataset = dataset.repeat(num_epochs)''' - '''dataset = dataset.batch(batch_size)''' - - iterator = dataset.make_one_shot_iterator() - features= iterator.get_next() - - return features - - return _input_fn - - - - - -read_file("./catvdogtest.tfrecords") - -'''tfrec_dev_input_fn = tfrec_data_input_fn(["catvdog.tfrecords"]) -features, labels = tfrec_dev_input_fn() - -with tf.Session() as sess: - img, label = sess.run([features['image'], labels]) - img = np.reshape(img, (img.shape[0], -1)) - print(img.shape) - for i in range(16): - plt.imshow(img[i]) - plt.show()''' - \ No newline at end of file From 1d3eb9c538513f1c69f3c7cd19aab1ace4044a8e Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:00:31 +0200 Subject: [PATCH 05/28] Delete Tfrecord.cpython-36.pyc --- .../tfRecord/__pycache__/Tfrecord.cpython-36.pyc | Bin 3599 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Work/tfRecord/__pycache__/Tfrecord.cpython-36.pyc diff --git a/Work/tfRecord/__pycache__/Tfrecord.cpython-36.pyc b/Work/tfRecord/__pycache__/Tfrecord.cpython-36.pyc deleted file mode 100644 index b256d391818616bea6ef6a8b64a54b8e549d78a9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3599 zcmbVPOOM;g5hj}tQItkAqxECPyGVr1Yqde-vAxJ4F^sjx@ooU`Y%nv6O^8_#w7Mlx zONn%ovS$Vq5J1k+L2}6{e;`19L9PLE&bhBSB>oF~Nxo`I@_05#kWgTEb+N0uy85fC z)}4(F|IfzXgHPIq@i*hb*(^EUdc{4IV3{WY=4uP)3J7F-W@#5HkU?C@(^#(`~b^Bukmjk}y38QphY!G5F5 zR4pIP27^Q>I~Rfzq;JrJv@T~pb^Bz@Agg4#^# zd8*E%BC1F?iJpig*Jf4C2GlD5wbET$i|}LEK5H=vXc>K4n;vqNjaU2neGr9Fm?d1b zVC3O~mFP|4v0ob}Y;Mi%(kgB3b@QNWDVq#c)_I&s(%E(77CO0&MwxwOlclPrjTHQ{ zm8}W{@pKr{yl+D&_s|%y)3cjQ?tt!-jr26|(mu4UJ2&c)cP{IxmDY0JQp<&T7xj9i@3IDNXA!fix@PbQ&eS&cW9N_Ih_#A_btomRT+G zUu5XiJJF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>S|srk+`)-b?y|SC zqzIay9fl9@AKwpucK`U}pt<~4qKmn{DeEh!uG>2Nl^MOx02=?V@4MRk3n8Ulpm*=x nouqu0h Date: Wed, 2 May 2018 18:00:52 +0200 Subject: [PATCH 06/28] Delete __init__.cpython-36.pyc --- Work/tfRecord/__pycache__/__init__.cpython-36.pyc | Bin 205 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Work/tfRecord/__pycache__/__init__.cpython-36.pyc diff --git a/Work/tfRecord/__pycache__/__init__.cpython-36.pyc b/Work/tfRecord/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index 14365e91ca043da0883b22aa9a2927f61aeaa8eb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 205 zcmXr!<>flW_&my#fq~&M5W@izkmUfx#WFx5g&~D8has0Sijfh-W&*OAqL_ee=3oX* zmY0k`C7O)4I6~5jQj_zGQh;p!6d)@lHNFHYn&Ma5v!&?v?wwK%&ZzaSt<8 From 165d4d54d0482789c04279f396039a8f86905ff4 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:01:01 +0200 Subject: [PATCH 07/28] Delete decode_tfrecord.cpython-36.pyc --- .../__pycache__/decode_tfrecord.cpython-36.pyc | Bin 3051 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Work/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc diff --git a/Work/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc b/Work/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc deleted file mode 100644 index 34952f566042af9c4999d1a4f01997fd772b8c41..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3051 zcmbtWTW{1x6!wg->ufGSproaAd!emcSZIK%LJ+D#^Uy*>3l-85WSpJZT`#+~JD!9j z+IB>v44c^_1gzaRg84(RY^)s;kcY6m(Zn_JvY_D@TfPWl-`FSnv}H2F=yPvq4ovvo z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-ctV-y4DdL*gWez!8{9 zI^zX0n|KEW;@5~olOhSV?+7v)I@e&3Igj16YF7D@CwVI1&*oWy zZ#FOw{EyHiQ#8%hv3?}8UMhR>2K=VrNI7M1W<8+C=GL;J%`J`l8W*%jQ%5=uobs;s bQ?}XTE9g^x90WuSZl8Frf6ia>18?SETyx}{ From b3d8b1bfa49b7bcc5a0d976999a53e447809f440 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:01:16 +0200 Subject: [PATCH 08/28] Delete NN.py --- Work/numpyrec/NN.py | 100 -------------------------------------------- 1 file changed, 100 deletions(-) delete mode 100644 Work/numpyrec/NN.py diff --git a/Work/numpyrec/NN.py b/Work/numpyrec/NN.py deleted file mode 100644 index 302af5d..0000000 --- a/Work/numpyrec/NN.py +++ /dev/null @@ -1,100 +0,0 @@ -import tensorflow as tf -import numpy as np -T_HEIGHT = 256 -T_WIDTH = 256 - -TRAIN_FILE = 'tftrain.tfrecords' -TEST_FILE = 'tftest.tfrecords' - -learning_rate = 0.001 -num_steps = 1500 -batch_size = 16 -display_step = 10 - -n_hidden_1 = 3 # 1st layer number of neurons -n_hidden_2 = 3 # 2nd layer number of neurons -num_input = 49152 # data input (img shape: 256*256) -num_classes = 2 # - -# tf Graph input -X = tf.placeholder("float", [None, num_input]) -Y = tf.placeholder(tf.int64, [None, num_classes]) - -# Store layers weight & bias -weights = { - 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), - 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), - 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) -} -biases = { - 'b1': tf.Variable(tf.random_normal([n_hidden_1])), - 'b2': tf.Variable(tf.random_normal([n_hidden_2])), - 'out': tf.Variable(tf.random_normal([num_classes])) -} - - -# Create model -def neural_net(x): - # Hidden fully connected layer with 256 neurons - layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) - z1 = tf.nn.relu(layer_1) - # Hidden fully connected layer with 256 neurons - layer_2 = tf.add(tf.matmul(z1, weights['h2']), biases['b2']) - # Output fully connected layer with a neuron for each class - '''z2 = tf.nn.relu(layer_2)''' - out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] - return out_layer - -# Construct model -logits = neural_net(X) -prediction = tf.nn.softmax(logits) - -# Define loss and optimizer -loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2( - labels=Y, logits=logits)) -optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) -train_op = optimizer.minimize(loss_op) - -# Evaluate model -correct_pred = tf.equal(prediction, tf.argmax(Y, 1)) -accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) -#### - - -# Initialize the variables (i.e. assign their default value) -init = tf.global_variables_initializer() - -# Start training -with tf.Session() as sess: - - # Run the initializer - sess.run(init) - - for step in range(1, num_steps+1): - img, label = sess.run([features['image'], labels]) - - # Run optimization op (backprop) - sess.run(train_op, feed_dict={X: img, Y: label}) - - if step % display_step == 0 or step == 1: - # Calculate batch loss and accuracy - loss, acc = sess.run([loss_op, accuracy], feed_dict={X: img, - Y: label}) - print("Step " + str(step) + ", Minibatch Loss= " + \ - "{:.4f}".format(loss) + ", Training Accuracy= " + \ - "{:.3f}".format(acc)) - - print("Optimization Finished!") - print("Testing Accuracy:", \ - sess.run(accuracy, feed_dict={X: img, - Y: label})) - for i in range(750): - img = sess.run([a['image']]) - print("prediction :", \ - sess.run(prediction, feed_dict={X:img})) - print(sess.run([weights['h1'], biases['b1'], weights['h2'], biases['b2']])) - print(sess.run([weights['out'],biases['out']])) - - - - \ No newline at end of file From 6e0fd71abdcce31a461942bf535b5a0bba092ea2 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:01:24 +0200 Subject: [PATCH 09/28] Delete nprecord.py --- Work/numpyrec/nprecord.py | 64 --------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 Work/numpyrec/nprecord.py diff --git a/Work/numpyrec/nprecord.py b/Work/numpyrec/nprecord.py deleted file mode 100644 index fe99af1..0000000 --- a/Work/numpyrec/nprecord.py +++ /dev/null @@ -1,64 +0,0 @@ -import os -import numpy as np -from glob import glob -import cv2 as cv -############### -# Cat vs Dog # -############### - -IMAGE_CHANNELS = 3 -IMAGE_HEIGHT = 128 -IMAGE_WIDTH = 128 -NUM_CLASS = 2 -gen_file_path = "train/train/" - -def get_paths(ur_path): - file_class1 = os.path.join(gen_file_path,'cat*.jpg' ) - file_class2 = os.path.join(gen_file_path, 'dog*.jpg') - cat_files = glob(file_class1) - dog_file = glob(file_class2) - size = len(cat_files) + len(dog_file) - return cat_files, dog_file, size - -def get_numpy(class1_files, class2_files, size): - dataX = np.zeros((size,IMAGE_HEIGHT, IMAGE_WIDTH,IMAGE_CHANNELS), dtype=np.float64) - dataY = np.zeros((size)) - i = 0 - for file in class1_files: - img = cv.imread(file) - img = cv.resize(img,(IMAGE_HEIGHT, IMAGE_WIDTH)) - img_np = np.array(img) - dataX[i] = img_np - dataY[i]= 0 - i += 1 - for file in class2_files: - try: - img = cv.imread(file) - img = cv.resize(img, (IMAGE_HEIGHT, IMAGE_WIDTH)) - img_np = np.array(img) - dataX[i] = img_np - dataY[i] = 1 - i +=1 - except: - continue - return dataX, dataY - -def make_npfile(dir, inputs, labels): - ###### - #Space dedicated for pre-processing methods - ###### - if (dir == ""): - dir = os.getcwd() - np.save(dir, [inputs, labels], ["inputs, labels"]) - return 0 - -def load_npfile(filename): - f = os.path.join(os.path.dirname(filename), filename) - data = np.load(f) - features = data[0] - labels = data[1] - return features, labels -alpha, beta, gamma = get_paths(gen_file_path) -a , b = get_numpy(alpha, beta, gamma) -make_npfile("", a, b) -'''a, b= load_npfile("train.npy")''' From 12c046e6b6d369a939f5ef1ce053de10919fe3fb Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:01:34 +0200 Subject: [PATCH 10/28] Delete norma.py --- Work/utils_data/norma/norma.py | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 Work/utils_data/norma/norma.py diff --git a/Work/utils_data/norma/norma.py b/Work/utils_data/norma/norma.py deleted file mode 100644 index a0c0fb6..0000000 --- a/Work/utils_data/norma/norma.py +++ /dev/null @@ -1,7 +0,0 @@ -import tensorflow as tf - - -def normalize(image, label): - """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" - image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 - return image, label \ No newline at end of file From 5a2619bfc9086058527e1f637b2215831363d8f0 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:01:42 +0200 Subject: [PATCH 11/28] Delete CNN_XRAY.py --- Work/CNN_XRAY.py | 227 ----------------------------------------------- 1 file changed, 227 deletions(-) delete mode 100644 Work/CNN_XRAY.py diff --git a/Work/CNN_XRAY.py b/Work/CNN_XRAY.py deleted file mode 100644 index 90da2f1..0000000 --- a/Work/CNN_XRAY.py +++ /dev/null @@ -1,227 +0,0 @@ -import tensorflow as tf -import tfRecord as trec - - -T_HEIGHT = 32 -T_WIDTH = 32 -T_channels = 3 - -TRAIN_FILE = 'tftrain.tfrecords' -TEST_FILE = 'tftest.tfrecords' - -learning_rate = 0.001 - -num_steps = 1500 - -batch_size = 8 - -display_step = 10 - - - -# Network Parameters - -num_input = T_HEIGHT * T_WIDTH * T_channels - -num_classes = 2 - - - - -# tf Graph input - -X = tf.placeholder(tf.float32, [None, num_input]) - -Y = tf.placeholder(tf.int64, [None, num_classes]) - - - - - - - -# Create some wrappers for simplicity - -def conv2d(x, W, b, strides=1): - - # Conv2D wrapper, with bias and relu activation - - x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') - - x = tf.nn.bias_add(x, b) - - return tf.nn.relu(x) - - - - - -def maxpool2d(x, k=2): - - # MaxPool2D wrapper - - return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], - - padding='SAME') - - - - - -# Create model - -def conv_net(x, weights, biases): - - - x = tf.reshape(x, shape=[-1, T_HEIGHT, T_WIDTH, T_channels]) - - - - # Convolution Layer - - conv1 = conv2d(x, weights['wc1'], biases['bc1']) - - # Max Pooling (down-sampling) - - conv1 = maxpool2d(conv1, k=2) - - - - # Convolution Layer - - conv2 = conv2d(conv1, weights['wc2'], biases['bc2']) - - # Max Pooling (down-sampling) - - conv2 = maxpool2d(conv2, k=2) - - - - - # Fully connected layer - - # Reshape conv2 output to fit fully connected layer input - - fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]]) - - fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) - - fc1 = tf.nn.relu(fc1) - - # Output, class prediction - - out = tf.add(tf.matmul(fc1, weights['out']), biases['out']) - - return out - - - -# Store layers weight & bias - -weights = { - - # 5x5 conv, 1 input, 32 outputs - - 'wc1': tf.Variable(tf.random_normal([3, 3, 3, 32])), - - # 5x5 conv, 32 inputs, 64 outputs - - 'wc2': tf.Variable(tf.random_normal([3, 3, 32, 64])), - - # fully connected, 32*32*64 inputs, 1024 outputs - - 'wd1': tf.Variable(tf.random_normal([8*8*64, 512])), - - # 1024 inputs, 10 outputs (class prediction) - - 'out': tf.Variable(tf.random_normal([512, num_classes])) - -} - - - -biases = { - - 'bc1': tf.Variable(tf.random_normal([32])), - - 'bc2': tf.Variable(tf.random_normal([64])), - - 'bd1': tf.Variable(tf.random_normal([512])), - - 'out': tf.Variable(tf.random_normal([num_classes])) - -} - - - -# Construct model - -logits = conv_net(X, weights, biases) - -prediction = tf.nn.softmax(logits) - - - -# Define loss and optimizer - -loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( - logits=logits, labels=Y)) - -optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) - -train_op = optimizer.minimize(loss_op) - - - - - -# Evaluate model - -correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1)) - -accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.int64)) - - - -# Initialize the variables (i.e. assign their default value) -tfrec_dev_input_fn = trec.decode_tfrecord.tfrec_data_input_fn(["catvdog.tfrecords"],batch_size=batch_size) -features, labels = tfrec_dev_input_fn() - -init = tf.global_variables_initializer() - - - -# Start training - -with tf.Session() as sess: - - - - # Run the initializer - - sess.run(init) - - - - for step in range(1, num_steps+1): - img, label = sess.run([features['image'], labels]) - - # Run optimization op (backprop) - sess.run(train_op, feed_dict={X: img, Y: label}) - - if step % display_step == 0 or step == 1: - # Calculate batch loss and accuracy - loss, acc = sess.run([loss_op, accuracy], feed_dict={X: img, - Y: label}) - print("Step " + str(step) + ", Minibatch Loss= " + \ - "{:.4f}".format(loss) + ", Training Accuracy= " + \ - "{:.3f}".format(acc)) - - print("Optimization Finished!") - '''tfrec_dev_input_fn = trec.decode_tfrecord.tfrec_data_input_fn(["catvdogtest.tfrecords"], batch_size=1000) - features, labels = tfrec_dev_input_fn() - img, label = sess.run([features['image'], labels]) - print("Testing Accuracy:", \ - sess.run(accuracy, feed_dict={X: img, - Y: label}))''' - sess.close() \ No newline at end of file From fb224fd623a531fbe234a6c52347e6c0028c61df Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:01:50 +0200 Subject: [PATCH 12/28] Delete NN-Xray.py --- Work/NN-Xray.py | 112 ------------------------------------------------ 1 file changed, 112 deletions(-) delete mode 100644 Work/NN-Xray.py diff --git a/Work/NN-Xray.py b/Work/NN-Xray.py deleted file mode 100644 index 40788bf..0000000 --- a/Work/NN-Xray.py +++ /dev/null @@ -1,112 +0,0 @@ -import tensorflow as tf -import numpy as np -import tfRecord as trec -T_HEIGHT = 256 -T_WIDTH = 256 - -TRAIN_FILE = 'tftrain.tfrecords' -TEST_FILE = 'tftest.tfrecords' - -learning_rate = 0.001 -num_steps = 1500 -batch_size = 16 -display_step = 10 - -n_hidden_1 = 3 # 1st layer number of neurons -n_hidden_2 = 3 # 2nd layer number of neurons -num_input = 3072 # data input (img shape: 256*256) -num_classes = 2 # - -# tf Graph input -X = tf.placeholder("float", [None, num_input]) -Y = tf.placeholder(tf.int64, [None, num_classes]) - -# Store layers weight & bias -weights = { - 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), - 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), - 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) -} -biases = { - 'b1': tf.Variable(tf.random_normal([n_hidden_1])), - 'b2': tf.Variable(tf.random_normal([n_hidden_2])), - 'out': tf.Variable(tf.random_normal([num_classes])) -} - - -# Create model -def neural_net(x): - # Hidden fully connected layer with 256 neurons - layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) - z1 = tf.nn.relu(layer_1) - # Hidden fully connected layer with 256 neurons - layer_2 = tf.add(tf.matmul(z1, weights['h2']), biases['b2']) - # Output fully connected layer with a neuron for each class - '''z2 = tf.nn.relu(layer_2)''' - out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] - return out_layer - -# Construct model -logits = neural_net(X) -prediction = tf.nn.softmax(logits) - -# Define loss and optimizer -loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2( - labels=Y, logits=logits)) -optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) -train_op = optimizer.minimize(loss_op) - -# Evaluate model -correct_pred = tf.equal(tf.argmax(prediction,1), tf.argmax(Y, 1)) -accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) -#### - - - - - -tfrec_dev_input_fn = trec.decode_tfrecord.tfrec_data_input_fn(["catvdog.tfrecords"]) -features, labels = tfrec_dev_input_fn() - -# Initialize the variables (i.e. assign their default value) -init = tf.global_variables_initializer() - -# Start training -with tf.Session() as sess: - - # Run the initializer - sess.run(init) - - for step in range(1, num_steps+1): - img, label = sess.run([features['image'], labels]) - - # Run optimization op (backprop) - sess.run(train_op, feed_dict={X: img, Y: label}) - - if step % display_step == 0 or step == 1: - # Calculate batch loss and accuracy - loss, acc = sess.run([loss_op, accuracy], feed_dict={X: img, - Y: label}) - print("Step " + str(step) + ", Minibatch Loss= " + \ - "{:.4f}".format(loss) + ", Training Accuracy= " + \ - "{:.3f}".format(acc)) - - print("Optimization Finished!") - tfrec_dev_input_fn = trec.decode_tfrecord.tfrec_data_input_fn(["catvdogtest.tfrecords"], batch_size=1000) - features, labels = tfrec_dev_input_fn() - img, label = sess.run([features['image'], labels]) - print("Testing Accuracy:", \ - sess.run(accuracy, feed_dict={X: img, - Y: label})) - '''tfrec_dcatvdog= trec.decode_tfrecord.tfrec_data_catvdog(["catvdogtest2.tfrecords"]) - a = tfrec_dcatvdog() - for i in range(750): - img = sess.run([a['image']]) - print("prediction :", \ - sess.run(prediction, feed_dict={X:img})) - print(sess.run([weights['h1'], biases['b1'], weights['h2'], biases['b2']])) - print(sess.run([weights['out'],biases['out']]))''' - - - - \ No newline at end of file From 1bf6206a5ad7f66cf747174300b3d349474edc33 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Wed, 2 May 2018 18:03:59 +0200 Subject: [PATCH 13/28] Add files via upload --- NN2-Xray.py | 128 +++++++++++ NN3-Xray..py | 122 ++++++++++ tfRecord/Tfrecord.py | 210 ++++++++++++++++++ tfRecord/__init__.py | 2 + tfRecord/__pycache__/Tfrecord.cpython-36.pyc | Bin 0 -> 4269 bytes tfRecord/__pycache__/__init__.cpython-36.pyc | Bin 0 -> 250 bytes .../decode_tfrecord.cpython-36.pyc | Bin 0 -> 2994 bytes tfRecord/decode_tfrecord.py | 129 +++++++++++ utils_data/colors/colors.py | 116 ++++++++++ utils_data/geotransf/geotransf.py | 81 +++++++ utils_data/norma/norma.py | 22 ++ 11 files changed, 810 insertions(+) create mode 100644 NN2-Xray.py create mode 100644 NN3-Xray..py create mode 100644 tfRecord/Tfrecord.py create mode 100644 tfRecord/__init__.py create mode 100644 tfRecord/__pycache__/Tfrecord.cpython-36.pyc create mode 100644 tfRecord/__pycache__/__init__.cpython-36.pyc create mode 100644 tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc create mode 100644 tfRecord/decode_tfrecord.py create mode 100644 utils_data/colors/colors.py create mode 100644 utils_data/geotransf/geotransf.py create mode 100644 utils_data/norma/norma.py diff --git a/NN2-Xray.py b/NN2-Xray.py new file mode 100644 index 0000000..c2164b1 --- /dev/null +++ b/NN2-Xray.py @@ -0,0 +1,128 @@ +import tensorflow as tf +import numpy as np +from tfRecord.decode_tfrecord import tfrec_data_input_fn, tfrec_data_catvdog +import matplotlib.pyplot as plt + +T_HEIGHT = 256 +T_WIDTH = 256 +T_Channnels = 1 +TRAIN_FILE = 'tftrain.tfrecords' +TEST_FILE = 'tftest.tfrecords' + +learning_rate = 0.1 +lambda_r = 0.01 +num_steps = 1500 +num_epoch = 3 +batch_size = 16 +display_step = 10 + +n_hidden_1 = 160 # 1st layer number of neurons +n_hidden_2 = 160 # 2nd layer number of neurons + +num_input = T_Channnels*T_HEIGHT*T_WIDTH # data input (img shape: 64*64) +num_classes = 2 # + +# tf Graph input +X = tf.placeholder("float", [None, num_input]) +Y = tf.placeholder(tf.int64, [None, num_classes]) + +# Store layers weight & bias +weights = { + 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), + 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), + 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) +} +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['h1']) +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['h2']) +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['out']) +biases = { + 'b1': tf.Variable(tf.random_normal([n_hidden_1])), + 'b2': tf.Variable(tf.random_normal([n_hidden_2])), + 'out': tf.Variable(tf.random_normal([num_classes])) +} + + +# Create model +def neural_net(x): + + layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) + z1 = tf.nn.tanh(layer_1) + + layer_2 = tf.add(tf.matmul(z1, weights['h2']), biases['b2']) + + z2 = tf.nn.relu(layer_2) + out_layer = tf.matmul(z2, weights['out']) + biases['out'] + return out_layer + +# Construct model +logits = neural_net(X) +prediction = tf.nn.softmax(logits) + +# Define loss and optimizer + +loss_op = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=logits) +regul = tf.contrib.layers.l2_regularizer(scale=0.1) +reg_variables = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) +reg_term = tf.contrib.layers.apply_regularization(regul, reg_variables) #Regularization term +loss_op = tf.reduce_mean(loss_op + reg_term) +optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) +train_op = optimizer.minimize(loss_op) + +# Evaluate model +correct_pred = tf.equal(tf.argmax(prediction,1), tf.argmax(Y, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) +#### + + + + + +tfrec_dev_input_fn = tfrec_data_input_fn(["tftrain.tfrecords"], num_epochs=num_epoch, shuffle=True) +iterator = tfrec_dev_input_fn() + +# Initialize the variables (i.e. assign their default value) +init = tf.global_variables_initializer() + +# Start training +with tf.Session() as sess: + sess.run(init) + for ep in range(1, num_epoch+1): + # Run the initializer + sess.run(iterator.initializer) + features, labels = iterator.get_next() + + img, label = sess.run([features['image'], labels]) + for step in range(1,1+num_steps): + '''for i in range(16): + print(label[i]) + plt.imshow(img[i].reshape(256,256)) + plt.show()''' + # Run optimization op (backprop) + sess.run(train_op, feed_dict={X: img, Y: label}) + + if step % display_step == 0 or step == 1: + # Calculate batch loss and accuracy + loss, acc = sess.run([loss_op, accuracy], feed_dict={X: img, + Y: label}) + print("Step " + str(step) + ", Minibatch Loss= " + \ + "{:.4f}".format(loss) + ", Training Accuracy= " + \ + "{:.3f}".format(acc)) + print("Optimization finished") + + + tfrec_dev_input_fn = tfrec_data_input_fn(["tftest.tfrecords"], batch_size=1000) + iterator = tfrec_dev_input_fn() + sess.run(iterator.initializer) + features, labels = iterator.get_next() + img, label = sess.run([features['image'], labels]) + print("Testing Accuracy:", \ + sess.run(accuracy, feed_dict={X: img, + Y: label})) + + + tfrec_dcatvdog= tfrec_data_catvdog(["tfpred.tfrecords"]) + a = tfrec_dcatvdog() + for i in range(750): + img = sess.run([a['image']]) + print("prediction :", \ + sess.run(prediction, feed_dict={X:img})) diff --git a/NN3-Xray..py b/NN3-Xray..py new file mode 100644 index 0000000..5b80f0b --- /dev/null +++ b/NN3-Xray..py @@ -0,0 +1,122 @@ +import tensorflow as tf +import numpy as np +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('MNIST_data', one_hot=True) +import matplotlib.pyplot as plt + +T_HEIGHT = 28 +T_WIDTH = 28 +T_Channnels = 1 +TRAIN_FILE = 'tftrain.tfrecords' +TEST_FILE = 'tftest.tfrecords' + +learning_rate = 0.1 +lambda_r = 0.01 +num_steps = 1500 +num_epoch = 3 +batch_size = 16 +display_step = 10 + +n_hidden_1 = 160 # 1st layer number of neurons +n_hidden_2 = 160 # 2nd layer number of neurons + +num_input = T_Channnels*T_HEIGHT*T_WIDTH # data input (img shape: 64*64) +num_classes = 10 # + +# tf Graph input +X = tf.placeholder("float", [None, num_input]) +Y = tf.placeholder(tf.int64, [None, num_classes]) + +# Store layers weight & bias +weights = { + 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), + 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), + 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) +} +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['h1']) +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['h2']) +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['out']) +biases = { + 'b1': tf.Variable(tf.random_normal([n_hidden_1])), + 'b2': tf.Variable(tf.random_normal([n_hidden_2])), + 'out': tf.Variable(tf.random_normal([num_classes])) +} + + +# Create model +def neural_net(x): + + layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) + z1 = tf.nn.tanh(layer_1) + + layer_2 = tf.add(tf.matmul(z1, weights['h2']), biases['b2']) + + z2 = tf.nn.relu(layer_2) + out_layer = tf.matmul(z2, weights['out']) + biases['out'] + return out_layer + +# Construct model +logits = neural_net(X) +prediction = tf.nn.softmax(logits) + +# Define loss and optimizer + +loss_op = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=logits) +regul = tf.contrib.layers.l2_regularizer(scale=0.1) +reg_variables = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) +reg_term = tf.contrib.layers.apply_regularization(regul, reg_variables) #Regularization term +loss_op = tf.reduce_mean(loss_op + reg_term) +optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) +train_op = optimizer.minimize(loss_op) + +# Evaluate model +correct_pred = tf.equal(tf.argmax(prediction,1), tf.argmax(Y, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) +#### + + +# Initialize the variables (i.e. assign their default value) +init = tf.global_variables_initializer() + +# Start training +with tf.Session() as sess: + sess.run(init) + # Run the initializer + '''img, label = sess.run([features['image'], labels])''' + batch = mnist.train.next_batch(batch_size) + print(batch[0][1].shape) + for step in range(1,1+num_steps): + + '''for i in range(16): + print(label[i]) + plt.imshow(img[i].reshape(256,256)) + plt.show()''' + # Run optimization op (backprop) + + '''sess.run(train_op, feed_dict={X: batch[0], Y: batch[1]}) + + if step % display_step == 0 or step == 1: + # Calculate batch loss and accuracy + loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch[0], + Y: batch[1]}) + print("Step " + str(step) + ", Minibatch Loss= " + \ + "{:.4f}".format(loss) + ", Training Accuracy= " + \ + "{:.3f}".format(acc)) + print("Optimization finished") + + + + print("Testing Accuracy:", \ + sess.run(accuracy, feed_dict={X: mnist.test.images, + Y: mnist.test.labels})) + + + tfrec_dcatvdog= tfrec_data_catvdog(["tfpred.tfrecords"]) + a = tfrec_dcatvdog() + for i in range(750): + img = sess.run([a['image']]) + print("prediction :", \ + sess.run(prediction, feed_dict={X:img})) + + print(sess.run([weights['h1'], biases['b1'], weights['h2'], biases['b2']])) + print(sess.run([weights['out'],biases['out']]))''' \ No newline at end of file diff --git a/tfRecord/Tfrecord.py b/tfRecord/Tfrecord.py new file mode 100644 index 0000000..fa1399e --- /dev/null +++ b/tfRecord/Tfrecord.py @@ -0,0 +1,210 @@ +from random import shuffle, seed +import os +import glob + +import numpy as np + +import cv2 + +import tensorflow as tf + +import sys + + + + +UR_DATA_PATH = "./train/*/*.jpg" +UR_PRED_PATH = "./test1/test1/*.jpg" + +UR_TEST_SIZE = 1000 + + + + +# This function returns the different paths and corresponding labels +def shuffling_data(data_path, labels): + c = list(zip(data_path, labels)) + + shuffle(c) + + #NOTE: data and labeld are tuples. Care about immutability + + data, labeled = zip(*c) #NOTE:ith element in data correpand to ith label elmt + + return data, labeled + +def read_paths_treat(ur_path, test_size): + pathname = os.path.dirname(ur_path) + #Reading the path of each data(ex: image) and then extracting the labels from path + data_path = glob.glob(ur_path) + '''labels = [int(i.split('.')[0][-1]) for i in data_path]'''#xray + labels = [i.split('\\')[-1] for i in data_path] #Cat vs Dog + labels_num = [0 if "cat" in i else 1 for i in labels] #Cat vs Dog + len_d = len(labels_num) + k = int(len_d/2) + a = int((len_d-test_size)/2) + b = int(len_d-(test_size/2)) + data_path_test, labels_test = data_path[a : k], labels_num[a:k] + data_path_test = data_path_test + data_path[b:] + labels_test = labels_test + labels_num[b:] + data_path_train,labels_train = (data_path[0:a]), (labels_num[0:a]) + data_path_train = data_path_train + data_path[k:b] + labels_train = labels_train + labels_num[k:b] + data_path_test, labels_test = shuffling_data(data_path_test, labels_test) + data_path_train, labels_train = shuffling_data(data_path_train, labels_train) + return data_path_test, labels_test,data_path_train, labels_train + + +def split_data(training_prop,test_prop, data, labels, dev_prop=0): + + ''' + - training_prop is required .numerical float < 1 + - dev_prop is optional. Numerical float < 0.3 + - test_prop is required. Numerical float < 0.3 + - data and labels are lists + ''' + + #NOTE: data is a list of paths of each image + + training_data = data[0:int(training_prop*len(data))] + + training_labels = labels[0:int(training_prop*len(labels))] + + + + if dev_prop != 0: + + dev_data = data[int(training_prop*len(data)):int((1-dev_prop)*len(data))] + + dev_labels = labels[int(training_prop*len(labels)):int((1-dev_prop)*len(labels))] + + test_data = data[int((1-dev_prop)*len(data)):] + + test_labels = labels[int((1-dev_prop)*len(labels)):] + + return training_data,training_labels, dev_data, dev_labels, test_data, test_labels + + else: + + test_data = data[int((1-test_prop)*len(data)):] + + test_labels = labels[int((1-test_prop)*len(labels)):] + + return training_data, training_labels, test_data, test_labels + + + +# Load image + +def load_image(data, s_width, s_height): + + #cv2 load data from data (value of one path) + + img_load = cv2.imread(data, cv2.IMREAD_GRAYSCALE) + + '''img_after = cv2.cvtColor(img_load, cv2.COLOR_BGR2GRAY)''' + + img= cv2.resize(img_load, dsize=(s_width, s_height), interpolation=cv2.INTER_CUBIC) + + img_f = img.tostring() + + return img_f + + +def _int64_feature(value): + + return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) + + + +def _bytes_feature(value): + + return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) + + + +def get_tfrecord_file(ur_path, test_size, s_width, s_height, *x_filename,): + + #NOTE:data is a tuple (list) of paths of each image + + #NOTE: data can come from the dev-set or test-set + + #NOTE: Same for labeled + + test_img, test_label, train_img, train_label = read_paths_treat(ur_path, test_size) + + file_path = x_filename[0] + + #Open a TFRecordWriter + + writer = tf.python_io.TFRecordWriter(file_path) + dog, cat= 0,0 + for i in range(len(train_img)): + if "cat" in train_img[i]: + cat +=1 + else: + dog +=1 + new_img = load_image(train_img[i], s_width, s_height) + + new_label = train_label[i] + + new_feature = { 'height': _int64_feature(s_height), + 'width' : _int64_feature(s_width), + 'label': _int64_feature(new_label), + 'image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + #NOTE: PLease refer to this url for defenirtion: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto + + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + + writer.write(example.SerializeToString()) + print(dog,cat) + writer.close() + sys.stdout.flush() + file_path = x_filename[1] + writer = tf.python_io.TFRecordWriter(file_path) + + for j in range(len(test_img)): + + new_img = load_image(test_img[j], s_width, s_height) + + new_label = test_label[j] + + new_feature = { 'height': _int64_feature(s_height), + 'width' : _int64_feature(s_width), + 'label': _int64_feature(new_label), + 'image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + #NOTE: PLease refer to this url for def: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto + + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + + writer.write(example.SerializeToString()) + + writer.close() + + sys.stdout.flush() + + return 0 + + +def get_tfrecord_pred(ur_path, s_width, s_height, x_filename,): + pathname = os.path.dirname(ur_path) + data_path = glob.glob(ur_path) + file_path = x_filename + writer = tf.python_io.TFRecordWriter(file_path) + for i in range(len(data_path)): + new_img = load_image(data_path[i], s_width, s_height) + new_feature = { 'height': _int64_feature(s_height), + 'width' : _int64_feature(s_width), + 'image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + writer.write(example.SerializeToString()) + writer.close() + sys.stdout.flush() + return 0 + + +'''get_tfrecord_pred(UR_PRED_PATH, 256, 256, "tfpred.tfrecords") +''''''get_tfrecord_file(UR_DATA_PATH, UR_TEST_SIZE, 256, 256, "tftrain.tfrecords", "tftest.tfrecords")''' \ No newline at end of file diff --git a/tfRecord/__init__.py b/tfRecord/__init__.py new file mode 100644 index 0000000..dab8f1b --- /dev/null +++ b/tfRecord/__init__.py @@ -0,0 +1,2 @@ +from . import Tfrecord +from . import decode_tfrecord \ No newline at end of file diff --git a/tfRecord/__pycache__/Tfrecord.cpython-36.pyc b/tfRecord/__pycache__/Tfrecord.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6446f71bb9044e55ae8e57687425f3d41c491da8 GIT binary patch literal 4269 zcmb_fTW=gm74E9OPS3^TvEw+q3mUB!!31z_%WY-CI>fjc9opb*6g8)0tbU z+lgcK@G=4M5(H?))BXVX6^MVpbG_~h`3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Em%#}eEE^m$NLlnFHuWRcSU|s9&r&aUDE~RS-=->UV;&tjjvXdz;QN z!9%9~tieRS48kDum|yT)q#lKC&Qpgjo8ohrYatqA!@I>Xc3qW6Mw?VT2k2jt5zOg^ zi^n{Nj{d`QBPN^Txsua*Hm1nP+=vxk;kK`}In1t#-RLomfHpw`=EiJh0dEn_TQ;&8 zGnoa91&pj2%VwmoBh4b)`t}<{V9;YCLf5#}|0g1&fCZ-}B7wE@IwI1!s6kfD!V-v4 z^XIEqs*&^|Oiw7}{#5<8LivoPol`Q1Q-*?QEY6_7FBpNJJmNfvh9fYObjAy$GjS0G z!q|v6lQIc)>OAbpNsPD^i#GM^9_te Ra~B*6{Cv;9;;;DY{{o=%+XDaq literal 0 HcmV?d00001 diff --git a/tfRecord/decode_tfrecord.py b/tfRecord/decode_tfrecord.py new file mode 100644 index 0000000..d2a5546 --- /dev/null +++ b/tfRecord/decode_tfrecord.py @@ -0,0 +1,129 @@ +#NOTE: refer to this url: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py +import tensorflow as tf +import numpy as np +import os +import matplotlib.pyplot as plt + + + + + + + +def tfrec_data_input_fn(filenames, num_epochs=1, batch_size=16, shuffle=False): + + def _input_fn(): + def _parse_record(tf_record): + features = { + 'image': tf.FixedLenFeature([], dtype=tf.string), + 'label': tf.FixedLenFeature([], dtype=tf.int64) + } + record = tf.parse_single_example(tf_record, features) + + image_raw = tf.decode_raw(record['image'], tf.uint8) + label = tf.one_hot(tf.cast(record['label'], tf.int32), depth=2) + + def _normalize(image_x): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image_x, tf.float32) * (1. / 255)-0.5 + return image + + image_raw = _normalize(image_raw) + '''image_raw = tf.reshape(image_raw, (32,32,3))'''#For vizualizing + return { 'image': image_raw}, label + + # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html + dataset = tf.data.TFRecordDataset(filenames) + dataset = dataset.map(_parse_record) + + dataset = dataset.repeat(num_epochs) + dataset = dataset.batch(batch_size) + + iterator = dataset.make_initializable_iterator() + '''features, labels = iterator.get_next()''' + + return iterator + + return _input_fn + + +def read_file(x_filename, x_capacity=800): + record_iterator = tf.python_io.tf_record_iterator(path=x_filename) + + example = tf.train.Example() + for str_rec in record_iterator: + example.ParseFromString(str_rec) + height = int(example.features.feature['height'] + .int64_list + .value[0]) + + width = int(example.features.feature['width'] + .int64_list + .value[0]) + + + img_string = (example.features.feature['image'] + .bytes_list + .value[0]) + + label = (example.features.feature['label'].int64_list.value[0]) + + + return 0 + + +def tfrec_data_catvdog(filenames, num_epochs=1, batch_size=16): + + def _input_fn(): + def _parse_record(tf_record): + features = { + 'image': tf.FixedLenFeature([], dtype=tf.string), + } + record = tf.parse_single_example(tf_record, features) + + image_raw = tf.decode_raw(record['image'], tf.uint8) + + + def _normalize(image): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image, tf.float32) * (1. / 255)-0.5 + return image + + image_n = _normalize(image_raw) + + + + return { 'image': image_n } + + # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html + dataset = tf.data.TFRecordDataset(filenames) + dataset = dataset.map(_parse_record) + + '''dataset = dataset.repeat(num_epochs)''' + '''dataset = dataset.batch(batch_size)''' + + iterator = dataset.make_one_shot_iterator() + features= iterator.get_next() + + return features + + return _input_fn + + + + + +'''read_file("./tftest.tfrecords") + +tfrec_dev_input_fn = tfrec_data_input_fn(["tftest.tfrecords"]) +features, labels = tfrec_dev_input_fn() + +with tf.Session() as sess: + for step in range(2): + img, label = sess.run([features['image'], labels]) + img = np.reshape(img, (16,64,64,3)) + + for i in range(16): + plt.imshow(img[i]) + plt.show()''' + \ No newline at end of file diff --git a/utils_data/colors/colors.py b/utils_data/colors/colors.py new file mode 100644 index 0000000..f690558 --- /dev/null +++ b/utils_data/colors/colors.py @@ -0,0 +1,116 @@ +import cv2 + +############# +#Color change +############# +#Changing color space: +def change_color_gray(frame): + change = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + return change + +def change_color_hsv(frame): + change = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + return change + +def change_color_rgb(frame): + change = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + return change + +#Object tracking: using a specified range of color +#Ex: lower_blue = np.array([110,50,50]); upper_blue = np.array([130,255,255]) +#lower_col & upper_col have to be numpy arrays +def track_color(frame, lower_col, upper_col): + hsv = change_color_hsv(frame) + mask = cv2.inRange(hsv, lower_col, upper_col) + img = cv2.bitwise_and(frame,frame,mask=mask) + return img + +############# +#Thresholding +############# + +#Images processed in this part must be grayscale +#Ex: if x>value_min --> white else black +# value_aft is given if pixel > value_thr + +def thresh_bin(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY) + return img + +def thresh_bin_inv(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY_INV) + return img + +def thresh_truc(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TRUNC) + return img + +def thresh_tozero(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TOZERO) + return img + +def thresh_tozero_inv(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TOZERO_INV) + return img + +#Adaptive thresholding +#block_h: means block size height, block_w means block size width + +##With binary thresholding + +def thresh_adapgauss_bin(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_BINARY, block_h, block_w ) + + return img + +def thresh_adapgauss_bininv(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_BINARY_INV, block_h, block_w ) + + return img + +def thresh_adapgauss_tunc(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_TRUNC, block_h, block_w ) + + return img + +def thresh_adapgauss_toz(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_TOZERO, block_h, block_w ) + + return img + +def thresh_adapgauss_tozinv(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_TOZERO_INV, block_h, block_w ) + + return img + +def thresh_adapmean_bin(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_BINARY, block_h,block_w) + return img + +def thresh_adapmean_trunc(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_TRUNC, block_w, block_h) + return img + +def thresh_adapmean_toz(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_TOZERO, block_h, block_w) + return img + +def thresh_adapmean_tozinv(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_TOZERO_INV, block_h, block_w) + return img + + +################# +#Otsu thresholding +################## + +#TODO: ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) \ No newline at end of file diff --git a/utils_data/geotransf/geotransf.py b/utils_data/geotransf/geotransf.py new file mode 100644 index 0000000..019618d --- /dev/null +++ b/utils_data/geotransf/geotransf.py @@ -0,0 +1,81 @@ +import cv2 +import numpy as np +import matplotlib.pyplot as plt +#Let's consider M the translation matrix. Given a translation vector (tx,ty), +# the matrix is defined as follows: M = [1 0 tx] +# [0 1 ty] + + +################## +# Translation +################## +def translate(frame, tx, ty): + dim = frame.shape + M = np.float32([[1,0,tx],[0,1,ty]]) + img = cv2.warpAffine(frame, M, dim) + return img + +################ +# Rotation +################ +#See reference: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html +#Angle is in degree +def rotate(frame, angle): + dim = (int(frame.shape[0]/2), int(frame.shape[1]/2)) + M = cv2.getRotationMatrix2D(dim, angle, 1) #Last argument is for scale + img = cv2.warpAffine(frame, M, dim) + return img + +####################### +# Affine transformation +####################### +# For this func , we need three points from the original image +# And the corresponding points in the output image +#list1: list of tuples of input image: 3 tuples// Same for list2 (output image) +def affine(frame, list1, list2): + '''assert type(list1) == list and len(list1)==3''' + col,row,_ = frame.shape + sub1, sub2 = [], [] + for i in list1: + sub1.append([i[0],i[1]]) + for j in list2: + sub2.append([j[0], j[1]]) + ps1 = np.float32(sub1) + ps2 = np.float32(sub2) + M = cv2.getAffineTransform(ps1,ps2) + img = cv2.warpAffine(frame, M, dsize=(col,row)) + return img + + + +####################### +# Perspective transformation +####################### + +def perspec(frame, list1, list2): + '''assert type(list1) == list and len(list1)==4''' + col,row,_ = frame.shape + sub1, sub2 = [], [] + for i in list1: + sub1.append([i[0],i[1]]) + for j in list2: + sub2.append([j[0], j[1]]) + ps1 = np.float32(sub1) + ps2 = np.float32(sub2) + M = cv2.getPerspectiveTransform(ps1,ps2) + img = cv2.warpPerspective(frame, M, dsize=(col,row)) + + return img + +'''a = cv2.imread("C:/Users/User12/Desktop/Work/train/train/cat.0.jpg", cv2.IMREAD_UNCHANGED) +b = perspec(a, [(50,50),(300,50),(50,300), (300,300)], [(0,0),(250,0),(0,250),(250,250)]) +plt.imshow(b) +plt.show()''' + + +####################### +# Morphological transformation +####################### +#TODO: + + diff --git a/utils_data/norma/norma.py b/utils_data/norma/norma.py new file mode 100644 index 0000000..e6cd714 --- /dev/null +++ b/utils_data/norma/norma.py @@ -0,0 +1,22 @@ +import tensorflow as tf +import cv2 + +def normalize(image, label): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 + return image, label + +#################### +# Image Gradient +#################### + +def laplacian(frame): + img = cv2.Laplacian(frame, cv2.CV_64F) + return img + +def sobelX(frame, kernel_size): + img = cv2.Sobel(frame, cv2.CV_64F, 1, 0, ksize=kernel_size) + return img + +def sobelY(frame, kernel_size): + img = cv2.Sobel(frame, cv2.CV_64F, 0,1,ksize=kernel_size) \ No newline at end of file From dbc28ec4a2638f407a44f511a6b33c3795f9bd53 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Thu, 3 May 2018 10:18:04 +0200 Subject: [PATCH 14/28] add --- utils_data/histogram/__init__.py | 0 utils_data/histogram/histogram.py | 16 ++++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 utils_data/histogram/__init__.py create mode 100644 utils_data/histogram/histogram.py diff --git a/utils_data/histogram/__init__.py b/utils_data/histogram/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils_data/histogram/histogram.py b/utils_data/histogram/histogram.py new file mode 100644 index 0000000..89b4fa4 --- /dev/null +++ b/utils_data/histogram/histogram.py @@ -0,0 +1,16 @@ +from matplotlib import pyplot as plt +import cv2 +import numpy as np + +def plot_histogram(frame, color='b', channels=[0], mask=None, histosize=[256], ranges=[0,256]): + histr = cv2.calcHist([frame], channels, mask, histosize, ranges) + plt.plot(histr, color=color) + plt.show() + +def plot_bgr_histogram(frame, mask=None, histosize=[256], ranges=[0, 256]): + color = ('b','g','r') + for i, col in enumerate(color): + histr = cv2.calcHist([frame], [i], mask, [256], ranges) + plt.plot(histr, color=col) + plt.xlim([0,256]) + plt.show() \ No newline at end of file From f21acc9006f0f5f900772f095534bf97d047bb91 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:44:54 +0200 Subject: [PATCH 15/28] Delete decode_tfrecord.py --- tfRecord/decode_tfrecord.py | 129 ------------------------------------ 1 file changed, 129 deletions(-) delete mode 100644 tfRecord/decode_tfrecord.py diff --git a/tfRecord/decode_tfrecord.py b/tfRecord/decode_tfrecord.py deleted file mode 100644 index d2a5546..0000000 --- a/tfRecord/decode_tfrecord.py +++ /dev/null @@ -1,129 +0,0 @@ -#NOTE: refer to this url: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py -import tensorflow as tf -import numpy as np -import os -import matplotlib.pyplot as plt - - - - - - - -def tfrec_data_input_fn(filenames, num_epochs=1, batch_size=16, shuffle=False): - - def _input_fn(): - def _parse_record(tf_record): - features = { - 'image': tf.FixedLenFeature([], dtype=tf.string), - 'label': tf.FixedLenFeature([], dtype=tf.int64) - } - record = tf.parse_single_example(tf_record, features) - - image_raw = tf.decode_raw(record['image'], tf.uint8) - label = tf.one_hot(tf.cast(record['label'], tf.int32), depth=2) - - def _normalize(image_x): - """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" - image = tf.cast(image_x, tf.float32) * (1. / 255)-0.5 - return image - - image_raw = _normalize(image_raw) - '''image_raw = tf.reshape(image_raw, (32,32,3))'''#For vizualizing - return { 'image': image_raw}, label - - # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html - dataset = tf.data.TFRecordDataset(filenames) - dataset = dataset.map(_parse_record) - - dataset = dataset.repeat(num_epochs) - dataset = dataset.batch(batch_size) - - iterator = dataset.make_initializable_iterator() - '''features, labels = iterator.get_next()''' - - return iterator - - return _input_fn - - -def read_file(x_filename, x_capacity=800): - record_iterator = tf.python_io.tf_record_iterator(path=x_filename) - - example = tf.train.Example() - for str_rec in record_iterator: - example.ParseFromString(str_rec) - height = int(example.features.feature['height'] - .int64_list - .value[0]) - - width = int(example.features.feature['width'] - .int64_list - .value[0]) - - - img_string = (example.features.feature['image'] - .bytes_list - .value[0]) - - label = (example.features.feature['label'].int64_list.value[0]) - - - return 0 - - -def tfrec_data_catvdog(filenames, num_epochs=1, batch_size=16): - - def _input_fn(): - def _parse_record(tf_record): - features = { - 'image': tf.FixedLenFeature([], dtype=tf.string), - } - record = tf.parse_single_example(tf_record, features) - - image_raw = tf.decode_raw(record['image'], tf.uint8) - - - def _normalize(image): - """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" - image = tf.cast(image, tf.float32) * (1. / 255)-0.5 - return image - - image_n = _normalize(image_raw) - - - - return { 'image': image_n } - - # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html - dataset = tf.data.TFRecordDataset(filenames) - dataset = dataset.map(_parse_record) - - '''dataset = dataset.repeat(num_epochs)''' - '''dataset = dataset.batch(batch_size)''' - - iterator = dataset.make_one_shot_iterator() - features= iterator.get_next() - - return features - - return _input_fn - - - - - -'''read_file("./tftest.tfrecords") - -tfrec_dev_input_fn = tfrec_data_input_fn(["tftest.tfrecords"]) -features, labels = tfrec_dev_input_fn() - -with tf.Session() as sess: - for step in range(2): - img, label = sess.run([features['image'], labels]) - img = np.reshape(img, (16,64,64,3)) - - for i in range(16): - plt.imshow(img[i]) - plt.show()''' - \ No newline at end of file From 41b00a3fd7ccb74728413277d16d6954286fe246 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:45:05 +0200 Subject: [PATCH 16/28] Delete Tfrecord.cpython-36.pyc --- tfRecord/__pycache__/Tfrecord.cpython-36.pyc | Bin 4269 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tfRecord/__pycache__/Tfrecord.cpython-36.pyc diff --git a/tfRecord/__pycache__/Tfrecord.cpython-36.pyc b/tfRecord/__pycache__/Tfrecord.cpython-36.pyc deleted file mode 100644 index 6446f71bb9044e55ae8e57687425f3d41c491da8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4269 zcmb_fTW=gm74E9OPS3^TvEw+q3mUB!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Te?FHEf1`PCWB=#6c5ioU^Q^YNwYT5g+YYvL-k%c# zfcZdb7!575=OZ83{mj#u Date: Thu, 3 May 2018 15:45:11 +0200 Subject: [PATCH 17/28] Delete __init__.cpython-36.pyc --- tfRecord/__pycache__/__init__.cpython-36.pyc | Bin 250 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tfRecord/__pycache__/__init__.cpython-36.pyc diff --git a/tfRecord/__pycache__/__init__.cpython-36.pyc b/tfRecord/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index acd70d6212ae83d5b3fae5227fce220a4d6fda52..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 250 zcmXr!<>l%-@+``ffq~&M5W@izkmUfx#WFx5g&~D8has0Sijfh-W&*OAqL_ee=3oX* zmY0k`C7O)4I6~5jQj_zGQh;p!6d)@lHNFHYn&Ma3~bsd@Qj`7ti3#n~nK1u-S5#U;KzG2!_|*+wxgi6x2g0Y#|=Mfu68#l@L< z>G8fkx+%s6x&|h?Mh1o!V0AGiX+dB&#KgyE=4F<|$LkeT-r}&y%}*)KNwovHv>4 Date: Thu, 3 May 2018 15:45:17 +0200 Subject: [PATCH 18/28] Delete decode_tfrecord.cpython-36.pyc --- .../__pycache__/decode_tfrecord.cpython-36.pyc | Bin 2994 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc diff --git a/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc b/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc deleted file mode 100644 index 570936efb2c0e03693e276e21fb0ad73e18a506b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2994 zcmbtWOK%)S5T5RN?)aI+iEI+WB1m8%v4Rt$2!ewY;{y*X5KyFzqse%?cXu|kGeb{1 zSzEIwM{*AM2O#kW_)C34;^Z4=E>P7o`v{6qB=@RodaHY?s=KScZ*I=b`R9H;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^XIEqs*&^|Oiw7}{#5<8LivoPol`Q1Q-*?QEY6_7FBpNJJmNfvh9fYObjAy$GjS0G z!q|v6lQIc)>OAbpNsPD^i#GM^9_te Ra~B*6{Cv;9;;;DY{{o=%+XDaq From 4837632c1b907a3c313b5402d0840fcd4dc59fc1 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:45:27 +0200 Subject: [PATCH 19/28] Delete Tfrecord.py --- tfRecord/Tfrecord.py | 210 ------------------------------------------- 1 file changed, 210 deletions(-) delete mode 100644 tfRecord/Tfrecord.py diff --git a/tfRecord/Tfrecord.py b/tfRecord/Tfrecord.py deleted file mode 100644 index fa1399e..0000000 --- a/tfRecord/Tfrecord.py +++ /dev/null @@ -1,210 +0,0 @@ -from random import shuffle, seed -import os -import glob - -import numpy as np - -import cv2 - -import tensorflow as tf - -import sys - - - - -UR_DATA_PATH = "./train/*/*.jpg" -UR_PRED_PATH = "./test1/test1/*.jpg" - -UR_TEST_SIZE = 1000 - - - - -# This function returns the different paths and corresponding labels -def shuffling_data(data_path, labels): - c = list(zip(data_path, labels)) - - shuffle(c) - - #NOTE: data and labeld are tuples. Care about immutability - - data, labeled = zip(*c) #NOTE:ith element in data correpand to ith label elmt - - return data, labeled - -def read_paths_treat(ur_path, test_size): - pathname = os.path.dirname(ur_path) - #Reading the path of each data(ex: image) and then extracting the labels from path - data_path = glob.glob(ur_path) - '''labels = [int(i.split('.')[0][-1]) for i in data_path]'''#xray - labels = [i.split('\\')[-1] for i in data_path] #Cat vs Dog - labels_num = [0 if "cat" in i else 1 for i in labels] #Cat vs Dog - len_d = len(labels_num) - k = int(len_d/2) - a = int((len_d-test_size)/2) - b = int(len_d-(test_size/2)) - data_path_test, labels_test = data_path[a : k], labels_num[a:k] - data_path_test = data_path_test + data_path[b:] - labels_test = labels_test + labels_num[b:] - data_path_train,labels_train = (data_path[0:a]), (labels_num[0:a]) - data_path_train = data_path_train + data_path[k:b] - labels_train = labels_train + labels_num[k:b] - data_path_test, labels_test = shuffling_data(data_path_test, labels_test) - data_path_train, labels_train = shuffling_data(data_path_train, labels_train) - return data_path_test, labels_test,data_path_train, labels_train - - -def split_data(training_prop,test_prop, data, labels, dev_prop=0): - - ''' - - training_prop is required .numerical float < 1 - - dev_prop is optional. Numerical float < 0.3 - - test_prop is required. Numerical float < 0.3 - - data and labels are lists - ''' - - #NOTE: data is a list of paths of each image - - training_data = data[0:int(training_prop*len(data))] - - training_labels = labels[0:int(training_prop*len(labels))] - - - - if dev_prop != 0: - - dev_data = data[int(training_prop*len(data)):int((1-dev_prop)*len(data))] - - dev_labels = labels[int(training_prop*len(labels)):int((1-dev_prop)*len(labels))] - - test_data = data[int((1-dev_prop)*len(data)):] - - test_labels = labels[int((1-dev_prop)*len(labels)):] - - return training_data,training_labels, dev_data, dev_labels, test_data, test_labels - - else: - - test_data = data[int((1-test_prop)*len(data)):] - - test_labels = labels[int((1-test_prop)*len(labels)):] - - return training_data, training_labels, test_data, test_labels - - - -# Load image - -def load_image(data, s_width, s_height): - - #cv2 load data from data (value of one path) - - img_load = cv2.imread(data, cv2.IMREAD_GRAYSCALE) - - '''img_after = cv2.cvtColor(img_load, cv2.COLOR_BGR2GRAY)''' - - img= cv2.resize(img_load, dsize=(s_width, s_height), interpolation=cv2.INTER_CUBIC) - - img_f = img.tostring() - - return img_f - - -def _int64_feature(value): - - return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) - - - -def _bytes_feature(value): - - return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) - - - -def get_tfrecord_file(ur_path, test_size, s_width, s_height, *x_filename,): - - #NOTE:data is a tuple (list) of paths of each image - - #NOTE: data can come from the dev-set or test-set - - #NOTE: Same for labeled - - test_img, test_label, train_img, train_label = read_paths_treat(ur_path, test_size) - - file_path = x_filename[0] - - #Open a TFRecordWriter - - writer = tf.python_io.TFRecordWriter(file_path) - dog, cat= 0,0 - for i in range(len(train_img)): - if "cat" in train_img[i]: - cat +=1 - else: - dog +=1 - new_img = load_image(train_img[i], s_width, s_height) - - new_label = train_label[i] - - new_feature = { 'height': _int64_feature(s_height), - 'width' : _int64_feature(s_width), - 'label': _int64_feature(new_label), - 'image': _bytes_feature(tf.compat.as_bytes(new_img)) - } - #NOTE: PLease refer to this url for defenirtion: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto - - example = tf.train.Example(features=tf.train.Features(feature=new_feature)) - - writer.write(example.SerializeToString()) - print(dog,cat) - writer.close() - sys.stdout.flush() - file_path = x_filename[1] - writer = tf.python_io.TFRecordWriter(file_path) - - for j in range(len(test_img)): - - new_img = load_image(test_img[j], s_width, s_height) - - new_label = test_label[j] - - new_feature = { 'height': _int64_feature(s_height), - 'width' : _int64_feature(s_width), - 'label': _int64_feature(new_label), - 'image': _bytes_feature(tf.compat.as_bytes(new_img)) - } - #NOTE: PLease refer to this url for def: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto - - example = tf.train.Example(features=tf.train.Features(feature=new_feature)) - - writer.write(example.SerializeToString()) - - writer.close() - - sys.stdout.flush() - - return 0 - - -def get_tfrecord_pred(ur_path, s_width, s_height, x_filename,): - pathname = os.path.dirname(ur_path) - data_path = glob.glob(ur_path) - file_path = x_filename - writer = tf.python_io.TFRecordWriter(file_path) - for i in range(len(data_path)): - new_img = load_image(data_path[i], s_width, s_height) - new_feature = { 'height': _int64_feature(s_height), - 'width' : _int64_feature(s_width), - 'image': _bytes_feature(tf.compat.as_bytes(new_img)) - } - example = tf.train.Example(features=tf.train.Features(feature=new_feature)) - writer.write(example.SerializeToString()) - writer.close() - sys.stdout.flush() - return 0 - - -'''get_tfrecord_pred(UR_PRED_PATH, 256, 256, "tfpred.tfrecords") -''''''get_tfrecord_file(UR_DATA_PATH, UR_TEST_SIZE, 256, 256, "tftrain.tfrecords", "tftest.tfrecords")''' \ No newline at end of file From 38c3238776ff139bfb878e82a060538d8b5556fe Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:45:33 +0200 Subject: [PATCH 20/28] Delete __init__.py --- tfRecord/__init__.py | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 tfRecord/__init__.py diff --git a/tfRecord/__init__.py b/tfRecord/__init__.py deleted file mode 100644 index dab8f1b..0000000 --- a/tfRecord/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from . import Tfrecord -from . import decode_tfrecord \ No newline at end of file From 494b76dbbf1e853e9ea66a6a7d02509d3d35712f Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:45:44 +0200 Subject: [PATCH 21/28] Delete colors.py --- utils_data/colors/colors.py | 116 ------------------------------------ 1 file changed, 116 deletions(-) delete mode 100644 utils_data/colors/colors.py diff --git a/utils_data/colors/colors.py b/utils_data/colors/colors.py deleted file mode 100644 index f690558..0000000 --- a/utils_data/colors/colors.py +++ /dev/null @@ -1,116 +0,0 @@ -import cv2 - -############# -#Color change -############# -#Changing color space: -def change_color_gray(frame): - change = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - return change - -def change_color_hsv(frame): - change = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) - return change - -def change_color_rgb(frame): - change = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - return change - -#Object tracking: using a specified range of color -#Ex: lower_blue = np.array([110,50,50]); upper_blue = np.array([130,255,255]) -#lower_col & upper_col have to be numpy arrays -def track_color(frame, lower_col, upper_col): - hsv = change_color_hsv(frame) - mask = cv2.inRange(hsv, lower_col, upper_col) - img = cv2.bitwise_and(frame,frame,mask=mask) - return img - -############# -#Thresholding -############# - -#Images processed in this part must be grayscale -#Ex: if x>value_min --> white else black -# value_aft is given if pixel > value_thr - -def thresh_bin(frame, value_thr, value_aft): - _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY) - return img - -def thresh_bin_inv(frame, value_thr, value_aft): - _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY_INV) - return img - -def thresh_truc(frame, value_thr, value_aft): - _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TRUNC) - return img - -def thresh_tozero(frame, value_thr, value_aft): - _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TOZERO) - return img - -def thresh_tozero_inv(frame, value_thr, value_aft): - _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TOZERO_INV) - return img - -#Adaptive thresholding -#block_h: means block size height, block_w means block size width - -##With binary thresholding - -def thresh_adapgauss_bin(frame, value_aft, block_h, block_w): - img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ - cv2.THRESH_BINARY, block_h, block_w ) - - return img - -def thresh_adapgauss_bininv(frame, value_aft, block_h, block_w): - img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ - cv2.THRESH_BINARY_INV, block_h, block_w ) - - return img - -def thresh_adapgauss_tunc(frame, value_aft, block_h, block_w): - img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ - cv2.THRESH_TRUNC, block_h, block_w ) - - return img - -def thresh_adapgauss_toz(frame, value_aft, block_h, block_w): - img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ - cv2.THRESH_TOZERO, block_h, block_w ) - - return img - -def thresh_adapgauss_tozinv(frame, value_aft, block_h, block_w): - img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ - cv2.THRESH_TOZERO_INV, block_h, block_w ) - - return img - -def thresh_adapmean_bin(frame, value_aft, block_h, block_w): - img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ - cv2.THRESH_BINARY, block_h,block_w) - return img - -def thresh_adapmean_trunc(frame, value_aft, block_h, block_w): - img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ - cv2.THRESH_TRUNC, block_w, block_h) - return img - -def thresh_adapmean_toz(frame, value_aft, block_h, block_w): - img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ - cv2.THRESH_TOZERO, block_h, block_w) - return img - -def thresh_adapmean_tozinv(frame, value_aft, block_h, block_w): - img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ - cv2.THRESH_TOZERO_INV, block_h, block_w) - return img - - -################# -#Otsu thresholding -################## - -#TODO: ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) \ No newline at end of file From 6374e840072eb023982b22cbfb38c33608c8aab1 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:45:53 +0200 Subject: [PATCH 22/28] Delete geotransf.py --- utils_data/geotransf/geotransf.py | 81 ------------------------------- 1 file changed, 81 deletions(-) delete mode 100644 utils_data/geotransf/geotransf.py diff --git a/utils_data/geotransf/geotransf.py b/utils_data/geotransf/geotransf.py deleted file mode 100644 index 019618d..0000000 --- a/utils_data/geotransf/geotransf.py +++ /dev/null @@ -1,81 +0,0 @@ -import cv2 -import numpy as np -import matplotlib.pyplot as plt -#Let's consider M the translation matrix. Given a translation vector (tx,ty), -# the matrix is defined as follows: M = [1 0 tx] -# [0 1 ty] - - -################## -# Translation -################## -def translate(frame, tx, ty): - dim = frame.shape - M = np.float32([[1,0,tx],[0,1,ty]]) - img = cv2.warpAffine(frame, M, dim) - return img - -################ -# Rotation -################ -#See reference: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html -#Angle is in degree -def rotate(frame, angle): - dim = (int(frame.shape[0]/2), int(frame.shape[1]/2)) - M = cv2.getRotationMatrix2D(dim, angle, 1) #Last argument is for scale - img = cv2.warpAffine(frame, M, dim) - return img - -####################### -# Affine transformation -####################### -# For this func , we need three points from the original image -# And the corresponding points in the output image -#list1: list of tuples of input image: 3 tuples// Same for list2 (output image) -def affine(frame, list1, list2): - '''assert type(list1) == list and len(list1)==3''' - col,row,_ = frame.shape - sub1, sub2 = [], [] - for i in list1: - sub1.append([i[0],i[1]]) - for j in list2: - sub2.append([j[0], j[1]]) - ps1 = np.float32(sub1) - ps2 = np.float32(sub2) - M = cv2.getAffineTransform(ps1,ps2) - img = cv2.warpAffine(frame, M, dsize=(col,row)) - return img - - - -####################### -# Perspective transformation -####################### - -def perspec(frame, list1, list2): - '''assert type(list1) == list and len(list1)==4''' - col,row,_ = frame.shape - sub1, sub2 = [], [] - for i in list1: - sub1.append([i[0],i[1]]) - for j in list2: - sub2.append([j[0], j[1]]) - ps1 = np.float32(sub1) - ps2 = np.float32(sub2) - M = cv2.getPerspectiveTransform(ps1,ps2) - img = cv2.warpPerspective(frame, M, dsize=(col,row)) - - return img - -'''a = cv2.imread("C:/Users/User12/Desktop/Work/train/train/cat.0.jpg", cv2.IMREAD_UNCHANGED) -b = perspec(a, [(50,50),(300,50),(50,300), (300,300)], [(0,0),(250,0),(0,250),(250,250)]) -plt.imshow(b) -plt.show()''' - - -####################### -# Morphological transformation -####################### -#TODO: - - From 7d52cf82b4eae288e56318f9e804a05bf20d9d5a Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:46:24 +0200 Subject: [PATCH 23/28] Delete norma.py --- utils_data/norma/norma.py | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 utils_data/norma/norma.py diff --git a/utils_data/norma/norma.py b/utils_data/norma/norma.py deleted file mode 100644 index e6cd714..0000000 --- a/utils_data/norma/norma.py +++ /dev/null @@ -1,22 +0,0 @@ -import tensorflow as tf -import cv2 - -def normalize(image, label): - """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" - image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 - return image, label - -#################### -# Image Gradient -#################### - -def laplacian(frame): - img = cv2.Laplacian(frame, cv2.CV_64F) - return img - -def sobelX(frame, kernel_size): - img = cv2.Sobel(frame, cv2.CV_64F, 1, 0, ksize=kernel_size) - return img - -def sobelY(frame, kernel_size): - img = cv2.Sobel(frame, cv2.CV_64F, 0,1,ksize=kernel_size) \ No newline at end of file From 52b8a24bb51c4089706d1b312f89c8aa34101ae8 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:50:15 +0200 Subject: [PATCH 24/28] Add files via upload --- image_pro/__init__.py | 1 + image_pro/parameters.py | 7 + image_pro/setup.py | 28 +++ image_pro/tfRecord/Tfrecord.py | 213 ++++++++++++++++++ image_pro/tfRecord/__init__.py | 1 + .../__pycache__/Tfrecord.cpython-36.pyc | Bin 0 -> 4269 bytes .../__pycache__/__init__.cpython-36.pyc | Bin 0 -> 250 bytes .../decode_tfrecord.cpython-36.pyc | Bin 0 -> 2994 bytes image_pro/tfRecord/decode.py | 129 +++++++++++ image_pro/utils_data/__init__.py | 1 + image_pro/utils_data/colors/__init__.py | 1 + image_pro/utils_data/colors/colors.py | 117 ++++++++++ image_pro/utils_data/contours/__init__.py | 1 + image_pro/utils_data/contours/contours.py | 128 +++++++++++ image_pro/utils_data/geotransf/__init__.py | 1 + image_pro/utils_data/geotransf/geotransf.py | 81 +++++++ image_pro/utils_data/norma/__init__.py | 1 + image_pro/utils_data/norma/norma.py | 22 ++ image_pro/utils_data/zooming/__init__.py | 1 + image_pro/utils_data/zooming/zoom.py | 1 + 20 files changed, 734 insertions(+) create mode 100644 image_pro/__init__.py create mode 100644 image_pro/parameters.py create mode 100644 image_pro/setup.py create mode 100644 image_pro/tfRecord/Tfrecord.py create mode 100644 image_pro/tfRecord/__init__.py create mode 100644 image_pro/tfRecord/__pycache__/Tfrecord.cpython-36.pyc create mode 100644 image_pro/tfRecord/__pycache__/__init__.cpython-36.pyc create mode 100644 image_pro/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc create mode 100644 image_pro/tfRecord/decode.py create mode 100644 image_pro/utils_data/__init__.py create mode 100644 image_pro/utils_data/colors/__init__.py create mode 100644 image_pro/utils_data/colors/colors.py create mode 100644 image_pro/utils_data/contours/__init__.py create mode 100644 image_pro/utils_data/contours/contours.py create mode 100644 image_pro/utils_data/geotransf/__init__.py create mode 100644 image_pro/utils_data/geotransf/geotransf.py create mode 100644 image_pro/utils_data/norma/__init__.py create mode 100644 image_pro/utils_data/norma/norma.py create mode 100644 image_pro/utils_data/zooming/__init__.py create mode 100644 image_pro/utils_data/zooming/zoom.py diff --git a/image_pro/__init__.py b/image_pro/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/parameters.py b/image_pro/parameters.py new file mode 100644 index 0000000..e61d3f4 --- /dev/null +++ b/image_pro/parameters.py @@ -0,0 +1,7 @@ +UR_DATA_PATH = "./train/*/*.jpg" +UR_PRED_PATH = "./test1/test1/*.jpg" +UR_OPTION = "gray" +UR_TEST_SIZE = 1000 +T_HEIGHT = 256 +T_WIDTH = 256 +T_Channnels = 1 \ No newline at end of file diff --git a/image_pro/setup.py b/image_pro/setup.py new file mode 100644 index 0000000..3572ce0 --- /dev/null +++ b/image_pro/setup.py @@ -0,0 +1,28 @@ +from setuptools import setup, find_packages + + +setup( +name='image_pro', +version='0.0.1', +description='Image processing and save into Tfrecords', +author='', +author_email='hilalyamine@gmail.com/alaa.el.bouchti@gmail.com', +license='Open', +packages=find_packages(include=['image_pro','image_pro.*','tfRecord', 'utils_data']), +packages_dir={'tfrecord':'tfRecord', + 'tfrecord.tfrecord':'tfRecord/tfrecord.py', + 'tfrecord.decode':'tfRecord/decode.py', + 'utils': 'utils_data', + 'utils.colors': 'utils_data/colors', + 'utils.colors.colors': 'utils_data/colors/colors.py', + 'utils.contours':'utils_data/contours', + 'utils.contours.contours':'utils_data/contours/contours.py', + 'utils.geotransf':'utils_data/geotransf', + 'utils.geotransf.geotransf':'utils_data/geotransf/geotransf.py', + 'utils.norma':'utils_data/norma', + 'utils.norma.norma':'utils_data/contours/norma.py', + 'utils.visualize':'utils_data/contours', + 'utils.visualize.visu':'utils_data/visualize/visu.py' +}, +scripts = ['parameters.py'] +) \ No newline at end of file diff --git a/image_pro/tfRecord/Tfrecord.py b/image_pro/tfRecord/Tfrecord.py new file mode 100644 index 0000000..718719b --- /dev/null +++ b/image_pro/tfRecord/Tfrecord.py @@ -0,0 +1,213 @@ +from random import shuffle, seed +import os +import glob +from .utils_data.contours.contours import draw_all_contours +import numpy as np + +import cv2 + +import tensorflow as tf + +import sys + + + + + + + + + +# This function returns the different paths and corresponding labels +def shuffling_data(data_path, labels): + c = list(zip(data_path, labels)) + + shuffle(c) + + #NOTE: data and labeld are tuples. Care about immutability + + data, labeled = zip(*c) #NOTE:ith element in data correpand to ith label elmt + + return data, labeled + +def read_paths_treat(ur_path, test_size): + pathname = os.path.dirname(ur_path) + #Reading the path of each data(ex: image) and then extracting the labels from path + data_path = glob.glob(ur_path) + '''labels = [int(i.split('.')[0][-1]) for i in data_path]'''#xray + #NOTE: labels and labels_num: modify split method to get labels// conditions "cat" or "dog" + labels = [i.split('\\')[-1] for i in data_path] #Cat vs Dog + labels_num = [0 if "cat" in i else 1 for i in labels] #Cat vs Dog + len_d = len(labels_num) + k = int(len_d/2) + a = int((len_d-test_size)/2) + b = int(len_d-(test_size/2)) + data_path_test, labels_test = data_path[a : k], labels_num[a:k] + data_path_test = data_path_test + data_path[b:] + labels_test = labels_test + labels_num[b:] + data_path_train,labels_train = (data_path[0:a]), (labels_num[0:a]) + data_path_train = data_path_train + data_path[k:b] + labels_train = labels_train + labels_num[k:b] + data_path_test, labels_test = shuffling_data(data_path_test, labels_test) + data_path_train, labels_train = shuffling_data(data_path_train, labels_train) + return data_path_test, labels_test,data_path_train, labels_train + + +def split_data(training_prop,test_prop, data, labels, dev_prop=0): + + ''' + - training_prop is required .numerical float < 1 + - dev_prop is optional. Numerical float < 0.3 + - test_prop is required. Numerical float < 0.3 + - data and labels are lists + ''' + + #NOTE: data is a list of paths of each image + + training_data = data[0:int(training_prop*len(data))] + + training_labels = labels[0:int(training_prop*len(labels))] + + + + if dev_prop != 0: + + dev_data = data[int(training_prop*len(data)):int((1-dev_prop)*len(data))] + + dev_labels = labels[int(training_prop*len(labels)):int((1-dev_prop)*len(labels))] + + test_data = data[int((1-dev_prop)*len(data)):] + + test_labels = labels[int((1-dev_prop)*len(labels)):] + + return training_data,training_labels, dev_data, dev_labels, test_data, test_labels + + else: + + test_data = data[int((1-test_prop)*len(data)):] + + test_labels = labels[int((1-test_prop)*len(labels)):] + + return training_data, training_labels, test_data, test_labels + +def load(ur_path, option): + + if option =="gray": + img = cv2.imread(ur_path,cv2.IMREAD_GRAYSCALE) + elif option =="unchanged": + img = cv2.imread(ur_path, cv2.IMREAD_UNCHANGED) + else: + img = cv2.imread(ur_path,cv2.IMREAD_COLOR) + return img + + +# Load image + +def load_image(data, s_width, s_height, option): + + #cv2 load data from data (value of one path) + + img_load = load(data, UR_OPTION) + + + '''img_after = cv2.cvtColor(img_load, cv2.COLOR_BGR2GRAY)''' + + img= cv2.resize(img_cont, dsize=(s_width, s_height), interpolation=cv2.INTER_CUBIC) + + img_f = img.tostring() + + return img_f + + +def _int64_feature(value): + + return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) + + + +def _bytes_feature(value): + + return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) + + + +def get_tfrecord_file(ur_path, test_size, s_width, s_height,option, *x_filename,): + + #NOTE:data is a tuple (list) of paths of each image + + #NOTE: data can come from the dev-set or test-set + + #NOTE: Same for labeled + + test_img, test_label, train_img, train_label = read_paths_treat(ur_path, test_size) + + file_path = x_filename[0] + + #Open a TFRecordWriter + + writer = tf.python_io.TFRecordWriter(file_path) + for i in range(len(train_img)): + new_img = load_image(train_img[i], s_width, s_height,option) + + new_label = train_label[i] + + new_feature = { 'height': _int64_feature(s_height), + 'width' : _int64_feature(s_width), + 'label': _int64_feature(new_label), + 'image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + #NOTE: PLease refer to this url for defenirtion: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto + + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + + writer.write(example.SerializeToString()) + + writer.close() + sys.stdout.flush() + file_path = x_filename[1] + writer = tf.python_io.TFRecordWriter(file_path) + + for j in range(len(test_img)): + + new_img = load_image(test_img[j], s_width, s_height,option) + + new_label = test_label[j] + + new_feature = { 'height': _int64_feature(s_height), + 'width' : _int64_feature(s_width), + 'label': _int64_feature(new_label), + 'image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + #NOTE: PLease refer to this url for def: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto + + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + + writer.write(example.SerializeToString()) + + writer.close() + + sys.stdout.flush() + + return 0 + + +def get_tfrecord_pred(ur_path, s_width, s_height,option, x_filename): + pathname = os.path.dirname(ur_path) + data_path = glob.glob(ur_path) + file_path = x_filename + writer = tf.python_io.TFRecordWriter(file_path) + for i in range(len(data_path)): + new_img = load_image(data_path[i], s_width, s_height,option) + new_feature = { 'height': _int64_feature(s_height), + 'width' : _int64_feature(s_width), + 'image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + writer.write(example.SerializeToString()) + writer.close() + sys.stdout.flush() + return 0 + + +'''get_tfrecord_pred(UR_PRED_PATH, 256, 256,UR_OPTION, "tfpred.tfrecords")''' +get_tfrecord_file(UR_DATA_PATH, UR_TEST_SIZE, 128, 128,UR_OPTION, "tftrain.tfrecords", "tftest.tfrecords") \ No newline at end of file diff --git a/image_pro/tfRecord/__init__.py b/image_pro/tfRecord/__init__.py new file mode 100644 index 0000000..6f492a6 --- /dev/null +++ b/image_pro/tfRecord/__init__.py @@ -0,0 +1 @@ +#NOTE diff --git a/image_pro/tfRecord/__pycache__/Tfrecord.cpython-36.pyc b/image_pro/tfRecord/__pycache__/Tfrecord.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6446f71bb9044e55ae8e57687425f3d41c491da8 GIT binary patch literal 4269 zcmb_fTW=gm74E9OPS3^TvEw+q3mUB!!31z_%WY-CI>fjc9opb*6g8)0tbU z+lgcK@G=4M5(H?))BXVX6^MVpbG_~h`3rf0?^Ms&&TNziM7R2M-A?tXQ{VaO^o?@a z55IUB{N|Ejd}W-wOq4&yll%oj7=rbTJ_}f%2b@uz_sqc5Z!56$+YTK4b^}+xy`Z4q ze&7o;C<-em2|FkYN4U?8pdvg`z@pf$xyj*CyWNYFl|+%)Iaye{lFG0utW%&a!q!FC#^ zvU=SlRpv=|Bx%lutqLMc!{#VV50%pkA4a`Iv6ix^HE?x9B>rwlAERGgYkhQZKZ#_b z<+bYvn^E#89gYqj4CSMPw7na(hEg2tx1}zujh?AWzUc0t)1)b}c}C(v7_7oQtHfmq z<_l6N3F zMrM3o8?%hXfcgY(xn%sAKPENv6+3uIiZB0ztHG3=jC$P^IOJ06F*@_~#(q2ZA@*_L7?@%oN^i;Cq-s8@N1X0vLVd$YsF> zT=1ER%4~(HBqJs5ytbynM3mNmOw$tvO*Az_jG?K$ny_?i!^!TVNBT-+&#z(RXu-DtYVVKs} z>({1AB6@NBI zJBXQgvG0;7p-~LVudOM_VNTzc1sbKn?ah{OZZv%`SU6SAKO5+gy2~e>X=^^IZZ7ID z%G>9`?Q7D})c+B_ovc6@#Z|s)dHg%L-@{$CNXl=ar$vW3T?Au-wTALl6zCuTkd2s6 zaIE2Nf?fgZHXsoLwlCPHoj7)s^48WP1D6f$P*Gb%fP=#C6 z`Bv+4^{I8oP?}lyW0YIuHoEA@Ai#fuC;0;$N{mJM(t$O$$IjRtd&GKR#X<_#!mR~l z1io-_E*Hn86kIuBkljxrwjtz(FuxqG51h` z^rlI?HcR4{XV{V2x0FdKcMv@#JyoDWu2rPs)Q`%IrYmIeJQFN-qO_S#lX`99Z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TaU%i%8aKGSO47x;_z3=;)S*&aSh3h@&@qSaLC=2yftA~%1wh;} z(fDHaEC4QZkQ8*&c-#hbGy!w0LqZJcz}W41jIJ2HTF*xu0$j1jdO)Bl{b;of+L?0o z(5);ula)j7=gSWPF#s!E#3U;VWmh>}9(B4>Amk|fdBkLQ0!xkAGB;}3zU-z+5T)wu zIIG83u52m7C{gZLRgEm%#}eEE^m$NLlnFHuWRcSU|s9&r&aUDE~RS-=->UV;&tjjvXdz;QN z!9%9~tieRS48kDum|yT)q#lKC&Qpgjo8ohrYatqA!@I>Xc3qW6Mw?VT2k2jt5zOg^ zi^n{Nj{d`QBPN^Txsua*Hm1nP+=vxk;kK`}In1t#-RLomfHpw`=EiJh0dEn_TQ;&8 zGnoa91&pj2%VwmoBh4b)`t}<{V9;YCLf5#}|0g1&fCZ-}B7wE@IwI1!s6kfD!V-v4 z^XIEqs*&^|Oiw7}{#5<8LivoPol`Q1Q-*?QEY6_7FBpNJJmNfvh9fYObjAy$GjS0G z!q|v6lQIc)>OAbpNsPD^i#GM^9_te Ra~B*6{Cv;9;;;DY{{o=%+XDaq literal 0 HcmV?d00001 diff --git a/image_pro/tfRecord/decode.py b/image_pro/tfRecord/decode.py new file mode 100644 index 0000000..d2a5546 --- /dev/null +++ b/image_pro/tfRecord/decode.py @@ -0,0 +1,129 @@ +#NOTE: refer to this url: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py +import tensorflow as tf +import numpy as np +import os +import matplotlib.pyplot as plt + + + + + + + +def tfrec_data_input_fn(filenames, num_epochs=1, batch_size=16, shuffle=False): + + def _input_fn(): + def _parse_record(tf_record): + features = { + 'image': tf.FixedLenFeature([], dtype=tf.string), + 'label': tf.FixedLenFeature([], dtype=tf.int64) + } + record = tf.parse_single_example(tf_record, features) + + image_raw = tf.decode_raw(record['image'], tf.uint8) + label = tf.one_hot(tf.cast(record['label'], tf.int32), depth=2) + + def _normalize(image_x): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image_x, tf.float32) * (1. / 255)-0.5 + return image + + image_raw = _normalize(image_raw) + '''image_raw = tf.reshape(image_raw, (32,32,3))'''#For vizualizing + return { 'image': image_raw}, label + + # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html + dataset = tf.data.TFRecordDataset(filenames) + dataset = dataset.map(_parse_record) + + dataset = dataset.repeat(num_epochs) + dataset = dataset.batch(batch_size) + + iterator = dataset.make_initializable_iterator() + '''features, labels = iterator.get_next()''' + + return iterator + + return _input_fn + + +def read_file(x_filename, x_capacity=800): + record_iterator = tf.python_io.tf_record_iterator(path=x_filename) + + example = tf.train.Example() + for str_rec in record_iterator: + example.ParseFromString(str_rec) + height = int(example.features.feature['height'] + .int64_list + .value[0]) + + width = int(example.features.feature['width'] + .int64_list + .value[0]) + + + img_string = (example.features.feature['image'] + .bytes_list + .value[0]) + + label = (example.features.feature['label'].int64_list.value[0]) + + + return 0 + + +def tfrec_data_catvdog(filenames, num_epochs=1, batch_size=16): + + def _input_fn(): + def _parse_record(tf_record): + features = { + 'image': tf.FixedLenFeature([], dtype=tf.string), + } + record = tf.parse_single_example(tf_record, features) + + image_raw = tf.decode_raw(record['image'], tf.uint8) + + + def _normalize(image): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image, tf.float32) * (1. / 255)-0.5 + return image + + image_n = _normalize(image_raw) + + + + return { 'image': image_n } + + # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html + dataset = tf.data.TFRecordDataset(filenames) + dataset = dataset.map(_parse_record) + + '''dataset = dataset.repeat(num_epochs)''' + '''dataset = dataset.batch(batch_size)''' + + iterator = dataset.make_one_shot_iterator() + features= iterator.get_next() + + return features + + return _input_fn + + + + + +'''read_file("./tftest.tfrecords") + +tfrec_dev_input_fn = tfrec_data_input_fn(["tftest.tfrecords"]) +features, labels = tfrec_dev_input_fn() + +with tf.Session() as sess: + for step in range(2): + img, label = sess.run([features['image'], labels]) + img = np.reshape(img, (16,64,64,3)) + + for i in range(16): + plt.imshow(img[i]) + plt.show()''' + \ No newline at end of file diff --git a/image_pro/utils_data/__init__.py b/image_pro/utils_data/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/colors/__init__.py b/image_pro/utils_data/colors/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/colors/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/colors/colors.py b/image_pro/utils_data/colors/colors.py new file mode 100644 index 0000000..a209e53 --- /dev/null +++ b/image_pro/utils_data/colors/colors.py @@ -0,0 +1,117 @@ +import cv2 + +############# +#Color change +############# +#Changing color space: +def change_color_gray(frame): + change = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + return change + +def change_color_hsv(frame): + change = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + return change + +def change_color_rgb(frame): + change = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + return change + +#Object tracking: using a specified range of color +#Ex: lower_blue = np.array([110,50,50]); upper_blue = np.array([130,255,255]) +#lower_col & upper_col have to be numpy arrays + +def track_color(frame, lower_col, upper_col): + hsv = change_color_hsv(frame) + mask = cv2.inRange(hsv, lower_col, upper_col) + img = cv2.bitwise_and(frame,frame,mask=mask) + return img + +############# +#Thresholding +############# + +#Images processed in this part must be grayscale +#Ex: if x>value_min --> white else black +# value_aft is given if pixel > value_thr + +def thresh_bin(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY) + return img + +def thresh_bin_inv(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY_INV) + return img + +def thresh_truc(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TRUNC) + return img + +def thresh_tozero(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TOZERO) + return img + +def thresh_tozero_inv(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TOZERO_INV) + return img + +#Adaptive thresholding +#block_h: means block size height, block_w means block size width + +##With binary thresholding + +def thresh_adapgauss_bin(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_BINARY, block_h, block_w ) + + return img + +def thresh_adapgauss_bininv(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_BINARY_INV, block_h, block_w ) + + return img + +def thresh_adapgauss_tunc(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_TRUNC, block_h, block_w ) + + return img + +def thresh_adapgauss_toz(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_TOZERO, block_h, block_w ) + + return img + +def thresh_adapgauss_tozinv(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_TOZERO_INV, block_h, block_w ) + + return img + +def thresh_adapmean_bin(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_BINARY, block_h,block_w) + return img + +def thresh_adapmean_trunc(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_TRUNC, block_w, block_h) + return img + +def thresh_adapmean_toz(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_TOZERO, block_h, block_w) + return img + +def thresh_adapmean_tozinv(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_TOZERO_INV, block_h, block_w) + return img + + +################# +#Otsu thresholding +################## + +#TODO: ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) \ No newline at end of file diff --git a/image_pro/utils_data/contours/__init__.py b/image_pro/utils_data/contours/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/contours/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/contours/contours.py b/image_pro/utils_data/contours/contours.py new file mode 100644 index 0000000..0b66f29 --- /dev/null +++ b/image_pro/utils_data/contours/contours.py @@ -0,0 +1,128 @@ +import numpy as np +import cv2 +'''from ..colors.colors import thresh_bin''' +import matplotlib.pyplot as plt + +#################### +# Canny contours +#################### +def canny_cont(frame, threch1, threch2): + img_save = frame + edges = cv2.Canny(frame, threch1,threch2) + return edges + + +################### +# Thresholding +#################### + +def thresh_bin(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY) + return img + +def find_contours(frame, approx=False): + tresh = thresh_bin(frame, 70, 255) + if approx: + image, contours, hierarchy = cv2.findContours(tresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + else: + image, contours, hierarchy = cv2.findContours(tresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) + + return image, contours, hierarchy + + #image: source image + # contours: contour retrieval mode + # hierarchy: contour approximation method + +def draw_all_contours(frame): + save_img = frame + tresh, contours, _ = find_contours(frame) + img = cv2.drawContours(tresh, contours, -1,(0,255)) + return img, save_img + +def draw_contour(frame, pos): + #pos is an int begining from 0 + save_img = frame + tresh, contours, _ = find_contours(frame) + image = cv2.drawContours(tresh, contours, pos, (0,255)) + return image, save_img + +def draw_contour_approx(frame): + save_img = frame + tresh, contours, _ = find_contours(frame, approx=True) + image = cv2.drawContours(tresh, contours, -1, (0,255)) + return image, save_img + +#################### +# Contours Features +#################### + +def get_moment(frame, index): + _, contours, _ = find_contours(frame, approx=True) + cnt = contours[index] + M = cv2.moments(cnt) + return M #M is a dict of all the moments + + +def get_contour_area(contour): + area = cv2.contourArea(contour) + return area + +def get_perimeter(contour): + perimeter = cv2.arcLength(contour) + return perimeter + +def get_approx_cont(contour): + epsilon = 0.1*cv2.arcLength(contour,True) + approx = cv2.approxPolyDP(contour,epsilon,True) + return approx + + +#################### +# Bounding Rectangle +#################### +def bounding_rec(frame): + _, contours,_ = find_contours(frame, approx=True) + image = frame + for c in contours: + x,y,w,h = cv2.boundingRect(c) + approx = get_approx_cont(c) + if len(approx) >= 4: + image = cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0)) + + return image + +def draw_rec(frame, index): + _,contours,_ = find_contours(frame, approx=True) + cnt = contours + rect = cv2.minAreaRect(cnt[0]) + box = cv2.boxPoints(rect) + box = np.int0(box) + image = cv2.drawContours(frame, [box],0,(0,0,255),2) + return image + + +######################## +# Contours Properties +######################## +def get_ratio(contour): + x,y,w,h = cv2.boundingRect(contour) + ratio = float(w)/h + return ratio + +def get_extent(contour): + area = get_contour_area(contour) + x,y,w,h = cv2.boundingRect(cnt) + rect_area = w*h + extent = float(area)/rect_area + return extent + +def mask_pix(frame_gray): + _,contours,_ = find_contours(frame_gray) + mask = np.zeros(frame_gray.shape,np.uint8) + cv2.drawContours(mask,contours,0,255) + pixelpoints = cv2.findNonZero(mask) + return pixelpoints + + +#Aspect ratio: It is the ratio of width to height of bounding rect of the object + diff --git a/image_pro/utils_data/geotransf/__init__.py b/image_pro/utils_data/geotransf/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/geotransf/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/geotransf/geotransf.py b/image_pro/utils_data/geotransf/geotransf.py new file mode 100644 index 0000000..019618d --- /dev/null +++ b/image_pro/utils_data/geotransf/geotransf.py @@ -0,0 +1,81 @@ +import cv2 +import numpy as np +import matplotlib.pyplot as plt +#Let's consider M the translation matrix. Given a translation vector (tx,ty), +# the matrix is defined as follows: M = [1 0 tx] +# [0 1 ty] + + +################## +# Translation +################## +def translate(frame, tx, ty): + dim = frame.shape + M = np.float32([[1,0,tx],[0,1,ty]]) + img = cv2.warpAffine(frame, M, dim) + return img + +################ +# Rotation +################ +#See reference: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html +#Angle is in degree +def rotate(frame, angle): + dim = (int(frame.shape[0]/2), int(frame.shape[1]/2)) + M = cv2.getRotationMatrix2D(dim, angle, 1) #Last argument is for scale + img = cv2.warpAffine(frame, M, dim) + return img + +####################### +# Affine transformation +####################### +# For this func , we need three points from the original image +# And the corresponding points in the output image +#list1: list of tuples of input image: 3 tuples// Same for list2 (output image) +def affine(frame, list1, list2): + '''assert type(list1) == list and len(list1)==3''' + col,row,_ = frame.shape + sub1, sub2 = [], [] + for i in list1: + sub1.append([i[0],i[1]]) + for j in list2: + sub2.append([j[0], j[1]]) + ps1 = np.float32(sub1) + ps2 = np.float32(sub2) + M = cv2.getAffineTransform(ps1,ps2) + img = cv2.warpAffine(frame, M, dsize=(col,row)) + return img + + + +####################### +# Perspective transformation +####################### + +def perspec(frame, list1, list2): + '''assert type(list1) == list and len(list1)==4''' + col,row,_ = frame.shape + sub1, sub2 = [], [] + for i in list1: + sub1.append([i[0],i[1]]) + for j in list2: + sub2.append([j[0], j[1]]) + ps1 = np.float32(sub1) + ps2 = np.float32(sub2) + M = cv2.getPerspectiveTransform(ps1,ps2) + img = cv2.warpPerspective(frame, M, dsize=(col,row)) + + return img + +'''a = cv2.imread("C:/Users/User12/Desktop/Work/train/train/cat.0.jpg", cv2.IMREAD_UNCHANGED) +b = perspec(a, [(50,50),(300,50),(50,300), (300,300)], [(0,0),(250,0),(0,250),(250,250)]) +plt.imshow(b) +plt.show()''' + + +####################### +# Morphological transformation +####################### +#TODO: + + diff --git a/image_pro/utils_data/norma/__init__.py b/image_pro/utils_data/norma/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/norma/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/norma/norma.py b/image_pro/utils_data/norma/norma.py new file mode 100644 index 0000000..e6cd714 --- /dev/null +++ b/image_pro/utils_data/norma/norma.py @@ -0,0 +1,22 @@ +import tensorflow as tf +import cv2 + +def normalize(image, label): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 + return image, label + +#################### +# Image Gradient +#################### + +def laplacian(frame): + img = cv2.Laplacian(frame, cv2.CV_64F) + return img + +def sobelX(frame, kernel_size): + img = cv2.Sobel(frame, cv2.CV_64F, 1, 0, ksize=kernel_size) + return img + +def sobelY(frame, kernel_size): + img = cv2.Sobel(frame, cv2.CV_64F, 0,1,ksize=kernel_size) \ No newline at end of file diff --git a/image_pro/utils_data/zooming/__init__.py b/image_pro/utils_data/zooming/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/zooming/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/zooming/zoom.py b/image_pro/utils_data/zooming/zoom.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/zooming/zoom.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file From 8a51cd8107e5d0a8617c640dcce0319c5b01e535 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:52:18 +0200 Subject: [PATCH 25/28] Rename utils_data/histogram/histogram.py to image_pro/utils_data/histogram/histogram.py --- {utils_data => image_pro/utils_data}/histogram/histogram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename {utils_data => image_pro/utils_data}/histogram/histogram.py (97%) diff --git a/utils_data/histogram/histogram.py b/image_pro/utils_data/histogram/histogram.py similarity index 97% rename from utils_data/histogram/histogram.py rename to image_pro/utils_data/histogram/histogram.py index 89b4fa4..7ef0af8 100644 --- a/utils_data/histogram/histogram.py +++ b/image_pro/utils_data/histogram/histogram.py @@ -13,4 +13,4 @@ def plot_bgr_histogram(frame, mask=None, histosize=[256], ranges=[0, 256]): histr = cv2.calcHist([frame], [i], mask, [256], ranges) plt.plot(histr, color=col) plt.xlim([0,256]) - plt.show() \ No newline at end of file + plt.show() From 0b89416bd54744bd5c3b3f596e9eb594dad7c357 Mon Sep 17 00:00:00 2001 From: Alaa El Bouchti Date: Thu, 3 May 2018 15:53:05 +0200 Subject: [PATCH 26/28] Rename utils_data/histogram/__init__.py to image_pro/utils_data/histogram/__init__.py --- image_pro/utils_data/histogram/__init__.py | 1 + utils_data/histogram/__init__.py | 0 2 files changed, 1 insertion(+) create mode 100644 image_pro/utils_data/histogram/__init__.py delete mode 100644 utils_data/histogram/__init__.py diff --git a/image_pro/utils_data/histogram/__init__.py b/image_pro/utils_data/histogram/__init__.py new file mode 100644 index 0000000..d3f5a12 --- /dev/null +++ b/image_pro/utils_data/histogram/__init__.py @@ -0,0 +1 @@ + diff --git a/utils_data/histogram/__init__.py b/utils_data/histogram/__init__.py deleted file mode 100644 index e69de29..0000000 From 4967fe3b922c9b5504d3d89179b7e09d55d0baac Mon Sep 17 00:00:00 2001 From: a-hilaly Date: Thu, 3 May 2018 21:33:33 +0200 Subject: [PATCH 27/28] Add makefile & requirements.txt --- Makefile | 56 ++++++++++++++++++ image_pro/tfRecord/Tfrecord.py | 9 +-- .../__pycache__/Tfrecord.cpython-36.pyc | Bin 4269 -> 0 bytes .../__pycache__/__init__.cpython-36.pyc | Bin 250 -> 0 bytes .../decode_tfrecord.cpython-36.pyc | Bin 2994 -> 0 bytes requirements.txt | 4 ++ CNN-Xray.py => scripts/CNN-Xray.py | 0 NN2-Xray.py => scripts/NN2-Xray.py | 0 NN3-Xray..py => scripts/NN3-Xray..py | 0 Tfrecord2.py => scripts/Tfrecord2.py | 0 .../decode_tfrecord.py | 0 image_pro/setup.py => setup.py | 19 +++--- 12 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 Makefile delete mode 100644 image_pro/tfRecord/__pycache__/Tfrecord.cpython-36.pyc delete mode 100644 image_pro/tfRecord/__pycache__/__init__.cpython-36.pyc delete mode 100644 image_pro/tfRecord/__pycache__/decode_tfrecord.cpython-36.pyc create mode 100644 requirements.txt rename CNN-Xray.py => scripts/CNN-Xray.py (100%) rename NN2-Xray.py => scripts/NN2-Xray.py (100%) rename NN3-Xray..py => scripts/NN3-Xray..py (100%) rename Tfrecord2.py => scripts/Tfrecord2.py (100%) rename decode_tfrecord.py => scripts/decode_tfrecord.py (100%) rename image_pro/setup.py => setup.py (69%) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f539ddc --- /dev/null +++ b/Makefile @@ -0,0 +1,56 @@ +# Makefile + +# Shell +SHELL := /bin/bash +#Project +PROJECT = "image_pro" +#Version +RELEASE = "0.0.1" +# Path to source directory +PROJECTPATH := . +# OS machine +OS_TYPE = 'uname -a' +# CPP EXTENSIONS +CPP_EXT = ".cpp" + +# SETUP +SETUPFILE = "setup.py" + +# VENV + +_virtualenv: + # create virtual env + virtualenv _virtualenv + _virtualenv/bin/pip install --upgrade pip + _virtualenv/bin/pip install --upgrade setuptools + +_use_env: + # use virtual env + source _virtualenv/bin/activate + +##### PYTHON + +prebuild: + @echo "Preparing build" + @echo "Installing requirements" + pip install -r requirements.txt + +setup: + echo "Setup..." + sudo python setup.py install + +test: + echo "Test" + python setup.py test + +clean: + rm -f MANIFEST + rm -rf build dist + +deactivate_env: + deactivate + + +all: prebuild setup + +.PHONY: clean \ No newline at end of file diff --git a/image_pro/tfRecord/Tfrecord.py b/image_pro/tfRecord/Tfrecord.py index 718719b..1451b4e 100644 --- a/image_pro/tfRecord/Tfrecord.py +++ b/image_pro/tfRecord/Tfrecord.py @@ -1,7 +1,7 @@ from random import shuffle, seed import os import glob -from .utils_data.contours.contours import draw_all_contours +from image_pro.utils_data.contours.contours import draw_all_contours import numpy as np import cv2 @@ -11,13 +11,6 @@ import sys - - - - - - - # This function returns the different paths and corresponding labels def shuffling_data(data_path, labels): c = list(zip(data_path, labels)) diff --git a/image_pro/tfRecord/__pycache__/Tfrecord.cpython-36.pyc b/image_pro/tfRecord/__pycache__/Tfrecord.cpython-36.pyc deleted file mode 100644 index 6446f71bb9044e55ae8e57687425f3d41c491da8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4269 zcmb_fTW=gm74E9OPS3^TvEw+q3mUB!!31z_%WY-CI>fjc9opb*6g8)0tbU z+lgcK@G=4M5(H?))BXVX6^MVpbG_~h`3rf0?^Ms&&TNziM7R2M-A?tXQ{VaO^o?@a z55IUB{N|Ejd}W-wOq4&yll%oj7=rbTJ_}f%2b@uz_sqc5Z!56$+YTK4b^}+xy`Z4q ze&7o;C<-em2|FkYN4U?8pdvg`z@pf$xyj*CyWNYFl|+%)Iaye{lFG0utW%&a!q!FC#^ zvU=SlRpv=|Bx%lutqLMc!{#VV50%pkA4a`Iv6ix^HE?x9B>rwlAERGgYkhQZKZ#_b z<+bYvn^E#89gYqj4CSMPw7na(hEg2tx1}zujh?AWzUc0t)1)b}c}C(v7_7oQtHfmq z<_l6N3F zMrM3o8?%hXfcgY(xn%sAKPENv6+3uIiZB0ztHG3=jC$P^IOJ06F*@_~#(q2ZA@*_L7?@%oN^i;Cq-s8@N1X0vLVd$YsF> zT=1ER%4~(HBqJs5ytbynM3mNmOw$tvO*Az_jG?K$ny_?i!^!TVNBT-+&#z(RXu-DtYVVKs} z>({1AB6@NBI zJBXQgvG0;7p-~LV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@)_j*;pLKy~8lI_W$ovhx8(^eSg8>_EVWueG< zP??^r9NTaB!Uq>>VJjMN_AO7gcqsP-S57WPlyu;Z68Y&ZwNx6)K^60W55j&HbLCgT zi}1O!_h991<)#S_x~UX1Xcpgw6LM5dwyP+V*DhV;$|>VgwetADioSob0I%vZEX6!p znhU14g0+^TFz7s4qOWCU{c^noVv0C_XtGuL5_?L63%zl_*)EVqR z*}I`^b=Adw_>{xZM>4`23wPmsBFRN4Q_(QRB@Et0b7;Vu%qU;KALMY$s@9HTo`ij# zD?i!m2Rut#-F#~MwejWR;pBKE2Jx0=iBEm#o_TQf%{H!WHf(Vf42TaU%i%8aKGSO47x;_z3=;)S*&aSh3h@&@qSaLC=2yftA~%1wh;} z(fDHaEC4QZkQ8*&c-#hbGy!w0LqZJcz}W41jIJ2HTF*xu0$j1jdO)Bl{b;of+L?0o z(5);ula)j7=gSWPF#s!E#3U;VWmh>}9(B4>Amk|fdBkLQ0!xkAGB;}3zU-z+5T)wu zIIG83u52m7C{gZLRgEm%#}eEE^m$NLlnFHuWRcSU|s9&r&aUDE~RS-=->UV;&tjjvXdz;QN z!9%9~tieRS48kDum|yT)q#lKC&Qpgjo8ohrYatqA!@I>Xc3qW6Mw?VT2k2jt5zOg^ zi^n{Nj{d`QBPN^Txsua*Hm1nP+=vxk;kK`}In1t#-RLomfHpw`=EiJh0dEn_TQ;&8 zGnoa91&pj2%VwmoBh4b)`t}<{V9;YCLf5#}|0g1&fCZ-}B7wE@IwI1!s6kfD!V-v4 z^XIEqs*&^|Oiw7}{#5<8LivoPol`Q1Q-*?QEY6_7FBpNJJmNfvh9fYObjAy$GjS0G z!q|v6lQIc)>OAbpNsPD^i#GM^9_te Ra~B*6{Cv;9;;;DY{{o=%+XDaq diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..eb13a03 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +tensorflow +opencv-python +numpy +matplotlib diff --git a/CNN-Xray.py b/scripts/CNN-Xray.py similarity index 100% rename from CNN-Xray.py rename to scripts/CNN-Xray.py diff --git a/NN2-Xray.py b/scripts/NN2-Xray.py similarity index 100% rename from NN2-Xray.py rename to scripts/NN2-Xray.py diff --git a/NN3-Xray..py b/scripts/NN3-Xray..py similarity index 100% rename from NN3-Xray..py rename to scripts/NN3-Xray..py diff --git a/Tfrecord2.py b/scripts/Tfrecord2.py similarity index 100% rename from Tfrecord2.py rename to scripts/Tfrecord2.py diff --git a/decode_tfrecord.py b/scripts/decode_tfrecord.py similarity index 100% rename from decode_tfrecord.py rename to scripts/decode_tfrecord.py diff --git a/image_pro/setup.py b/setup.py similarity index 69% rename from image_pro/setup.py rename to setup.py index 3572ce0..58145ae 100644 --- a/image_pro/setup.py +++ b/setup.py @@ -2,14 +2,14 @@ setup( -name='image_pro', -version='0.0.1', -description='Image processing and save into Tfrecords', -author='', -author_email='hilalyamine@gmail.com/alaa.el.bouchti@gmail.com', -license='Open', -packages=find_packages(include=['image_pro','image_pro.*','tfRecord', 'utils_data']), -packages_dir={'tfrecord':'tfRecord', + name='image_pro', + version='0.0.1', + description='Image processing and save into Tfrecords', + author='', + author_email='hilalyamine@gmail.com/alaa.el.bouchti@gmail.com', + license='Open', + packages=find_packages(include=['image_pro','image_pro.*','tfRecord', 'utils_data']), + packages_dir={'tfrecord':'tfRecord', 'tfrecord.tfrecord':'tfRecord/tfrecord.py', 'tfrecord.decode':'tfRecord/decode.py', 'utils': 'utils_data', @@ -23,6 +23,5 @@ 'utils.norma.norma':'utils_data/contours/norma.py', 'utils.visualize':'utils_data/contours', 'utils.visualize.visu':'utils_data/visualize/visu.py' -}, -scripts = ['parameters.py'] + }, ) \ No newline at end of file From b6396dec5a3adf290a69b6539e30d46e171754db Mon Sep 17 00:00:00 2001 From: a-hilaly Date: Thu, 3 May 2018 21:41:01 +0200 Subject: [PATCH 28/28] add --- image_pro/tfRecord/Tfrecord.py | 47 +++------------------ image_pro/tfRecord/decode.py | 18 ++------ image_pro/utils_data/contours/contours.py | 4 +- image_pro/utils_data/geotransf/geotransf.py | 2 +- 4 files changed, 11 insertions(+), 60 deletions(-) diff --git a/image_pro/tfRecord/Tfrecord.py b/image_pro/tfRecord/Tfrecord.py index 1451b4e..fff4917 100644 --- a/image_pro/tfRecord/Tfrecord.py +++ b/image_pro/tfRecord/Tfrecord.py @@ -1,24 +1,19 @@ -from random import shuffle, seed import os +import cv2 +import sys import glob -from image_pro.utils_data.contours.contours import draw_all_contours import numpy as np - -import cv2 - import tensorflow as tf -import sys +from random import shuffle, seed +from image_pro.utils_data.contours.contours import draw_all_contours # This function returns the different paths and corresponding labels def shuffling_data(data_path, labels): c = list(zip(data_path, labels)) - shuffle(c) - #NOTE: data and labeld are tuples. Care about immutability - data, labeled = zip(*c) #NOTE:ith element in data correpand to ith label elmt return data, labeled @@ -47,40 +42,27 @@ def read_paths_treat(ur_path, test_size): def split_data(training_prop,test_prop, data, labels, dev_prop=0): - ''' - training_prop is required .numerical float < 1 - dev_prop is optional. Numerical float < 0.3 - test_prop is required. Numerical float < 0.3 - data and labels are lists ''' - #NOTE: data is a list of paths of each image training_data = data[0:int(training_prop*len(data))] - training_labels = labels[0:int(training_prop*len(labels))] - - if dev_prop != 0: - dev_data = data[int(training_prop*len(data)):int((1-dev_prop)*len(data))] - dev_labels = labels[int(training_prop*len(labels)):int((1-dev_prop)*len(labels))] - test_data = data[int((1-dev_prop)*len(data)):] - test_labels = labels[int((1-dev_prop)*len(labels)):] - return training_data,training_labels, dev_data, dev_labels, test_data, test_labels else: - test_data = data[int((1-test_prop)*len(data)):] - test_labels = labels[int((1-test_prop)*len(labels)):] - return training_data, training_labels, test_data, test_labels def load(ur_path, option): @@ -99,14 +81,9 @@ def load(ur_path, option): def load_image(data, s_width, s_height, option): #cv2 load data from data (value of one path) - img_load = load(data, UR_OPTION) - - '''img_after = cv2.cvtColor(img_load, cv2.COLOR_BGR2GRAY)''' - img= cv2.resize(img_cont, dsize=(s_width, s_height), interpolation=cv2.INTER_CUBIC) - img_f = img.tostring() return img_f @@ -127,32 +104,25 @@ def _bytes_feature(value): def get_tfrecord_file(ur_path, test_size, s_width, s_height,option, *x_filename,): #NOTE:data is a tuple (list) of paths of each image - #NOTE: data can come from the dev-set or test-set - #NOTE: Same for labeled test_img, test_label, train_img, train_label = read_paths_treat(ur_path, test_size) - file_path = x_filename[0] - #Open a TFRecordWriter writer = tf.python_io.TFRecordWriter(file_path) for i in range(len(train_img)): new_img = load_image(train_img[i], s_width, s_height,option) - new_label = train_label[i] - new_feature = { 'height': _int64_feature(s_height), 'width' : _int64_feature(s_width), 'label': _int64_feature(new_label), 'image': _bytes_feature(tf.compat.as_bytes(new_img)) } - #NOTE: PLease refer to this url for defenirtion: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto + #NOTE: PLease refer to this url for defenirtion: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto example = tf.train.Example(features=tf.train.Features(feature=new_feature)) - writer.write(example.SerializeToString()) writer.close() @@ -163,24 +133,19 @@ def get_tfrecord_file(ur_path, test_size, s_width, s_height,option, *x_filename, for j in range(len(test_img)): new_img = load_image(test_img[j], s_width, s_height,option) - new_label = test_label[j] - new_feature = { 'height': _int64_feature(s_height), 'width' : _int64_feature(s_width), 'label': _int64_feature(new_label), 'image': _bytes_feature(tf.compat.as_bytes(new_img)) } - #NOTE: PLease refer to this url for def: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto + #NOTE: PLease refer to this url for def: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto example = tf.train.Example(features=tf.train.Features(feature=new_feature)) - writer.write(example.SerializeToString()) writer.close() - sys.stdout.flush() - return 0 diff --git a/image_pro/tfRecord/decode.py b/image_pro/tfRecord/decode.py index d2a5546..13e113a 100644 --- a/image_pro/tfRecord/decode.py +++ b/image_pro/tfRecord/decode.py @@ -1,14 +1,9 @@ -#NOTE: refer to this url: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py -import tensorflow as tf -import numpy as np import os +import numpy as np +import tensorflow as tf import matplotlib.pyplot as plt - - - - - +#NOTE: refer to this url: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py def tfrec_data_input_fn(filenames, num_epochs=1, batch_size=16, shuffle=False): @@ -83,16 +78,12 @@ def _parse_record(tf_record): image_raw = tf.decode_raw(record['image'], tf.uint8) - def _normalize(image): """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" image = tf.cast(image, tf.float32) * (1. / 255)-0.5 return image image_n = _normalize(image_raw) - - - return { 'image': image_n } # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html @@ -110,9 +101,6 @@ def _normalize(image): return _input_fn - - - '''read_file("./tftest.tfrecords") tfrec_dev_input_fn = tfrec_data_input_fn(["tftest.tfrecords"]) diff --git a/image_pro/utils_data/contours/contours.py b/image_pro/utils_data/contours/contours.py index 0b66f29..e503be6 100644 --- a/image_pro/utils_data/contours/contours.py +++ b/image_pro/utils_data/contours/contours.py @@ -15,7 +15,6 @@ def canny_cont(frame, threch1, threch2): ################### # Thresholding #################### - def thresh_bin(frame, value_thr, value_aft): _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY) return img @@ -28,7 +27,7 @@ def find_contours(frame, approx=False): image, contours, hierarchy = cv2.findContours(tresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) return image, contours, hierarchy - + #image: source image # contours: contour retrieval mode # hierarchy: contour approximation method @@ -123,6 +122,5 @@ def mask_pix(frame_gray): pixelpoints = cv2.findNonZero(mask) return pixelpoints - #Aspect ratio: It is the ratio of width to height of bounding rect of the object diff --git a/image_pro/utils_data/geotransf/geotransf.py b/image_pro/utils_data/geotransf/geotransf.py index 019618d..9c9b2fb 100644 --- a/image_pro/utils_data/geotransf/geotransf.py +++ b/image_pro/utils_data/geotransf/geotransf.py @@ -1,11 +1,11 @@ import cv2 import numpy as np import matplotlib.pyplot as plt + #Let's consider M the translation matrix. Given a translation vector (tx,ty), # the matrix is defined as follows: M = [1 0 tx] # [0 1 ty] - ################## # Translation ##################