Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
QIS_ImageClassification_ECCV20/test.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
135 lines (86 sloc)
3.27 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras | |
from keras.applications import VGG16 | |
from keras import layers | |
from keras.models import Model | |
from keras.preprocessing.image import ImageDataGenerator | |
from keras import optimizers | |
import numpy as np | |
np.random.seed(5) | |
RN = 0.25 | |
ME = 0.5 | |
def clean_img(img): | |
img = np.array(img) | |
img = img.astype('float') / 255.0; | |
return img | |
def add_noise(img): | |
rn = RN | |
me = ME | |
img = np.array(img) | |
img = img.astype('float') / 255.0; | |
s = (np.shape(img)) | |
mimg = np.zeros((s[0], s[1])) | |
mimg[0::2, 0::2] = img[0::2, 0::2, 0]; | |
mimg[1::2, 0::2] = img[1::2, 0::2, 1]; | |
mimg[0::2, 1::2] = img[0::2, 1::2, 1]; | |
mimg[1::2, 1::2] = img[1::2, 1::2, 2]; | |
alpha = me / np.mean(mimg); | |
img = alpha * mimg; | |
nimg = np.random.poisson(img) + np.random.normal(0, rn, (s[0], s[1])) | |
nimg = np.round(nimg) / alpha | |
nimg[nimg < 0] = 0 | |
img[nimg > 31] = 31 #5 bits | |
bimg = np.zeros((s[0], s[1], 1)) | |
bimg[:, :, 0] = nimg | |
return bimg | |
input1 = layers.Input((256,256, 1)) | |
demosai = layers.Conv2D(32, (5, 5), activation='relu', padding='same')(input1) | |
demosai = layers.Conv2D(3, (5, 5), activation='relu', padding='same')(demosai) | |
val_datagen = ImageDataGenerator(preprocessing_function=clean_img ) | |
def generate_generator_multiple(generator, dir, batch_size, img_height, img_width): | |
genX1 = generator.flow_from_directory( | |
dir, | |
target_size=(img_height, img_width), | |
batch_size=batch_size, | |
class_mode='categorical', | |
shuffle=False) | |
while True: | |
X1i = genX1.next() | |
img = X1i[0] * 255.0; | |
s = np.shape(img) | |
IMG = np.zeros((s[0], s[1], s[2], 1)) | |
l = np.shape(img) | |
for i in range(0, l[0]): | |
temp = np.zeros((img_height, img_width, 3)); | |
temp[:, :, :] = img[i, :, :, :]; | |
temp1 = add_noise(temp) | |
IMG[i, :, :, :] = temp1 | |
yield IMG, X1i[1] | |
def custom_objective(y_true, y_pred): | |
sum2 = keras.losses.categorical_crossentropy(y_true, y_pred) | |
return sum2 | |
def custom_metric(y_true, y_pred): | |
sum3 = keras.metrics.categorical_accuracy(y_true, y_pred) | |
return sum3 | |
test_generator = generate_generator_multiple(generator=val_datagen, | |
dir='data/clean/new_hard/test', | |
batch_size=24, | |
img_height=256, | |
img_width=256) | |
conv_base1 = VGG16(weights='imagenet', | |
include_top=False, | |
input_shape=(256, 256, 3)) | |
cb1 = Model(inputs=conv_base1.input, outputs=[conv_base1.output]) | |
cb1= cb1(demosai) | |
flat2 = layers.Flatten()(cb1) | |
dense1 = layers.Dense(512, activation='relu')(flat2) | |
dense1 = layers.Dense(512, activation='relu')(dense1) | |
dense2 = layers.Dense(10, activation='softmax')(dense1) | |
model = Model(inputs=[input1], outputs=[dense2]) | |
model.load_weights('model_zoo/QIS_Me_0.5.h5') | |
model.compile(loss=[custom_objective], | |
loss_weights=[1], | |
optimizer=optimizers.RMSprop(lr=1e-6, decay=4e-4), | |
metrics={(dense2.name).split('/')[0]: [custom_metric]}) | |
vv = model.evaluate_generator(test_generator, steps=20) | |
acc = vv[1] | |
print("Accuracy : ", acc*100,"%") |