Skip to content
Snippets Groups Projects
Commit 6a9b519c authored by Hashim Sharif's avatar Hashim Sharif
Browse files

Merging

parents 802c501f d188cfd9
No related branches found
No related tags found
No related merge requests found
import os
import glob
import random
import scipy
import scipy.io
import cv2
import numpy as np
import tensorflow as tf
import keras
from keras.models import Sequential, Model
from keras.layers import *
......@@ -35,21 +37,21 @@ VAL_SIZE = 100
# def relu6(x):
# return K.relu(x, max_value=6)
def relu6(x):
return K.relu(x, max_value=6)
def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1)):
channel_axis = 1
filters = int(filters * alpha)
x = ZeroPadding2D(padding=((0, 1), (0, 1)))(inputs)
x = ZeroPadding2D(padding=((1, 1), (1, 1)))(inputs)
x = Conv2D(filters, kernel,
padding='valid',
use_bias=False,
strides=strides)(x)
x = BatchNormalization(axis=channel_axis)(x)
return Activation('relu')(x)
return Activation(relu6)(x)
def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha,
......@@ -58,7 +60,7 @@ def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha,
pointwise_conv_filters = int(pointwise_conv_filters * alpha)
if strides != (1, 1):
x = ZeroPadding2D(padding=(1, 1))(inputs)
x = ZeroPadding2D(padding=((1, 1), (1, 1)))(inputs)
else:
x = inputs
......@@ -68,14 +70,14 @@ def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha,
strides=strides,
use_bias=False)(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = Activation(relu6)(x)
x = Conv2D(pointwise_conv_filters, (1, 1),
padding='same',
use_bias=False,
strides=(1, 1))(x)
x = BatchNormalization(axis=channel_axis)(x)
return Activation('relu')(x)
return Activation(relu6)(x)
......@@ -112,11 +114,13 @@ def get_mobilenet_nchw_keras():
x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13)
x = GlobalAveragePooling2D()(x)
x = Dropout(dropout)(x)
x = Dense(1000)(x)
x = AveragePooling2D((7, 7))(x)
x = Conv2D(1000, (1, 1),
padding='same')(x)
x = Flatten()(x)
x = Activation('softmax')(x)
model = Model(img_input, x)
......@@ -129,6 +133,7 @@ def get_mobilenet_nchw_keras():
try:
model.layers[j].set_weights(original_model.layers[i].get_weights())
print (j, 'loaded')
# model.layers[j].trainable = False
j += 1
except:
print (j, 'skipped', model.layers[j])
......@@ -205,6 +210,53 @@ y_true = np.array(y_true)
def train_helper(x):
try:
x = x.decode('utf-8')
except:
pass
image = load_image(x)
y = np.zeros(1000, dtype=np.uint8)
y[synset_to_keras_idx[x.split('/')[-2]]]= 1
return image, y
train_images = glob.glob(IMAGENET_DIR + 'train/*/*')
random.shuffle(train_images)
dataset = tf.data.Dataset().from_tensor_slices(train_images)
dataset = dataset.map(
lambda x : tf.py_func(train_helper, [x], [tf.float32, tf.uint8]),
num_parallel_calls=16
)
dataset = dataset.shuffle(buffer_size=1000)
dataset = dataset.batch(64)
dataset = dataset.repeat()
next_element = dataset.make_one_shot_iterator().get_next()
sess = tf.Session()
def generate():
while True:
yield sess.run(next_element)
model.compile(optimizer=keras.optimizers.Adam(lr=0.00001), loss='categorical_crossentropy', metrics=['acc'])
model.fit_generator(generate(), steps_per_epoch=1000, validation_data=(X_test, to_categorical(y_true, num_classes=1000)), epochs=5)
translate_to_approxhpvm(model, OUTPUT_DIR, X_test[:VAL_SIZE], y_true[:VAL_SIZE], 1000)
dumpCalibrationData(OUTPUT_DIR + 'test_input.bin', X_test, OUTPUT_DIR + 'test_labels.bin', y_true)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment