Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c9f5b25
Add files via upload
aelbouchti Apr 30, 2018
8843890
Delete Tfrecord.py
aelbouchti May 2, 2018
24f5747
Delete __init__.py
aelbouchti May 2, 2018
025bd34
Delete decode_tfrecord.py
aelbouchti May 2, 2018
1d3eb9c
Delete Tfrecord.cpython-36.pyc
aelbouchti May 2, 2018
d1ec549
Delete __init__.cpython-36.pyc
aelbouchti May 2, 2018
165d4d5
Delete decode_tfrecord.cpython-36.pyc
aelbouchti May 2, 2018
b3d8b1b
Delete NN.py
aelbouchti May 2, 2018
6e0fd71
Delete nprecord.py
aelbouchti May 2, 2018
12c046e
Delete norma.py
aelbouchti May 2, 2018
5a2619b
Delete CNN_XRAY.py
aelbouchti May 2, 2018
fb224fd
Delete NN-Xray.py
aelbouchti May 2, 2018
1bf6206
Add files via upload
aelbouchti May 2, 2018
dbc28ec
add
a-hilaly May 3, 2018
f21acc9
Delete decode_tfrecord.py
aelbouchti May 3, 2018
41b00a3
Delete Tfrecord.cpython-36.pyc
aelbouchti May 3, 2018
ff0e54e
Delete __init__.cpython-36.pyc
aelbouchti May 3, 2018
b6e846e
Delete decode_tfrecord.cpython-36.pyc
aelbouchti May 3, 2018
4837632
Delete Tfrecord.py
aelbouchti May 3, 2018
38c3238
Delete __init__.py
aelbouchti May 3, 2018
494b76d
Delete colors.py
aelbouchti May 3, 2018
6374e84
Delete geotransf.py
aelbouchti May 3, 2018
7d52cf8
Delete norma.py
aelbouchti May 3, 2018
52b8a24
Add files via upload
aelbouchti May 3, 2018
8a51cd8
Rename utils_data/histogram/histogram.py to image_pro/utils_data/hist…
aelbouchti May 3, 2018
0b89416
Rename utils_data/histogram/__init__.py to image_pro/utils_data/histo…
aelbouchti May 3, 2018
4967fe3
Add makefile & requirements.txt
a-hilaly May 3, 2018
b6396de
add
a-hilaly May 3, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions image_pro/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#NOTE
7 changes: 7 additions & 0 deletions image_pro/parameters.py
Original file line number Diff line number Diff line change
@@ -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
171 changes: 171 additions & 0 deletions image_pro/tfRecord/Tfrecord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import os
import cv2
import sys
import glob
import numpy as np
import tensorflow as tf

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

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")
1 change: 1 addition & 0 deletions image_pro/tfRecord/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#NOTE
117 changes: 117 additions & 0 deletions image_pro/tfRecord/decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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):

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()'''

1 change: 1 addition & 0 deletions image_pro/utils_data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#NOTE
1 change: 1 addition & 0 deletions image_pro/utils_data/colors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#NOTE
Loading