Skip to content
Snippets Groups Projects
Commit b919f9e2 authored by nz11's avatar nz11
Browse files

Add new file

parent cbb8ef05
No related branches found
No related tags found
No related merge requests found
import os
import glob
import scipy
import scipy.io
import cv2
import numpy as np
import keras
from keras.models import Sequential, Model
from keras.layers import *
from keras.applications.resnet50 import ResNet50, preprocess_input
from keras.utils import to_categorical
from keras import backend as K
from frontend.approxhpvm_translator import translate_to_approxhpvm
from frontend.weight_utils import dumpCalibrationData
np.random.seed(2020)
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
K.set_image_data_format('channels_first')
num_images = 1000
val_size = 100
data_format = 'channels_first'
def identity_block(input_tensor, kernel_size, filters, stage, block):
filters1, filters2, filters3 = filters
bn_axis = 1
x = Conv2D(filters1, (1, 1))(input_tensor)
x = BatchNormalization(axis=bn_axis)(x)
x = Activation('relu')(x)
x = Conv2D(filters2, kernel_size,
padding='same')(x)
x = BatchNormalization(axis=bn_axis)(x)
x = Activation('relu')(x)
x = Conv2D(filters3, (1, 1))(x)
x = BatchNormalization(axis=bn_axis)(x)
x = add([x, input_tensor])
x = Activation('relu')(x)
return x
def conv_block(input_tensor,
kernel_size,
filters,
stage,
block,
strides=(2, 2)):
filters1, filters2, filters3 = filters
bn_axis = 1
x = Conv2D(filters1, (1, 1), strides=strides)(input_tensor)
x = BatchNormalization(axis=bn_axis)(x)
x = Activation('relu')(x)
x = Conv2D(filters2, kernel_size, padding='same')(x)
x = BatchNormalization(axis=bn_axis)(x)
x = Activation('relu')(x)
x = Conv2D(filters3, (1, 1))(x)
x = BatchNormalization(axis=bn_axis)(x)
shortcut = Conv2D(filters3, (1, 1), strides=strides)(input_tensor)
shortcut = BatchNormalization(
axis=bn_axis)(shortcut)
x = add([x, shortcut])
x = Activation('relu')(x)
return x
def get_resnet50_nchw_keras():
img_input = Input(shape=(3, 224, 224))
bn_axis = 1
x = ZeroPadding2D((3, 3))(img_input)
x = Conv2D(64, (7, 7), strides=(2, 2))(x)
x = BatchNormalization(axis=bn_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
x = AveragePooling2D((7, 7))(x)
x = Flatten()(x)
x = Dense(1000)(x)
x = Activation('softmax')(x)
model = Model(img_input, x)
return model
# def get_resnet50_nchw_keras():
# model = ResNet50()
# for x in model.layers:
# print (x.name)
# x = model.get_layer('flatten_1').output
# x = Dense(1000, name='fc1000')(x)
# x = Activation('softmax')(x)
# model_nchw = Model(model.input, x)
# model_nchw.get_layer('fc1000').set_weights(model.get_layer('fc1000').get_weights())
# return model_nchw
def load_image(x):
image = cv2.imread(x)
height, width, _ = image.shape
new_height = height * 256 // min(image.shape[:2])
new_width = width * 256 // min(image.shape[:2])
image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
height, width, _ = image.shape
startx = width // 2 - (224 // 2)
starty = height // 2 - (224 // 2)
image = image[starty:starty + 224, startx:startx + 224]
image = image[:, :, ::-1]
image = np.transpose(image, (2, 0, 1))
image = preprocess_input(image.astype(np.float32), data_format=data_format)
return image.astype(np.float32)
# model = ResNet50()
model_nchw = get_resnet50_nchw_keras()
# i = 0
# j = 0
# for k in range(len(model_nchw.layers)):
# if 'pool' in model.layers[j].name or 'pad' in model.layers[j].name:
# j += 1
# elif 'pool' in model_nchw.layers[i].name or 'pad' in model_nchw.layers[i].name:
# i += 1
# else:
# model_nchw.layers[i].set_weights(model.layers[j].get_weights())
# i += 1
# j += 1
classes = os.listdir('/home/nz11/ILSVRC2012/train')
train_images = glob.glob('/home/nz11/ILSVRC2012/train/*/*')
val_images = glob.glob('/home/nz11/ILSVRC2012/val/*/*')
val_images = sorted(val_images, key=lambda x: x.split('/')[-1].split('_')[-1].split('.')[0])
idx = np.random.permutation(len(val_images))[:num_images]
val_images = np.array(val_images)[idx]
d = {k:v for v, k in enumerate(classes)}
X_test = []
for x in val_images:
X_test.append(load_image(x))
X_test = np.array(X_test)
meta = scipy.io.loadmat("/home/nz11/ILSVRC2012/ILSVRC2012_devkit_t12/data/meta.mat")
original_idx_to_synset = {}
synset_to_name = {}
for i in range(1000):
ilsvrc2012_id = int(meta["synsets"][i,0][0][0][0])
synset = meta["synsets"][i,0][1][0]
name = meta["synsets"][i,0][2][0]
original_idx_to_synset[ilsvrc2012_id] = synset
synset_to_name[synset] = name
synset_to_keras_idx = {}
keras_idx_to_name = {}
f = open("/home/nz11/ILSVRC2012/ILSVRC2012_devkit_t12/data/synset_words.txt","r")
c = 0
for line in f:
parts = line.split(" ")
synset_to_keras_idx[parts[0]] = c
keras_idx_to_name[c] = " ".join(parts[1:])
c += 1
f.close()
def convert_original_idx_to_keras_idx(idx):
return synset_to_keras_idx[original_idx_to_synset[idx]]
with open("/home/nz11/ILSVRC2012/ILSVRC2012_devkit_t12/data/ILSVRC2012_validation_ground_truth.txt","r") as f:
y_true = f.read().strip().split("\n")
y_true = list(map(int, y_true))
y_true = np.array([convert_original_idx_to_keras_idx(idx) for idx in y_true])[idx]
y_true = y_true.astype(np.uint32)
y_true = np.expand_dims(y_true, axis=-1)
translate_to_approxhpvm(model_nchw, "data/resnet50_imagenet/", X_test[:val_size], y_true[:val_size], 1000)
dumpCalibrationData("data/resnet50_imagenet/test_input.bin", X_test, "data/resnet50_imagenet/test_labels.bin", y_true)
pred = np.argmax(model_nchw.predict(X_test), axis=1)
print ('val accuracy', np.sum(pred == y_true.ravel()) / len(X_test))
\ No newline at end of file
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