-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvalScale2.py
98 lines (73 loc) · 3.41 KB
/
EvalScale2.py
1
import numpy as npimport osimport refrom keras.models import Model, load_modelfrom keras.preprocessing.image import load_img, img_to_arrayfrom keras import applicationsfrom keras.layers import Dropout, Conv2D, Reshape, Dense, Flatten, AvgPool2D, MaxPool2Dfrom keras.layers.normalization import BatchNormalizationfrom keras.applications.resnet50 import preprocess_inputimport matplotlib.pyplot as pltimg_width, img_height = 224*9, 224*9# generates a crop from one imagedef eval_generator(img_dir): file_names = [f for f in os.listdir(img_dir) if re.match(r'[0-9]+.*\.jpg', f)] file_names = sorted(file_names, key=lambda item: (int(item.partition('.')[0]) if item[0].isdigit() else float('inf'), item)) for filename in file_names: img = load_img(os.path.join(img_dir, filename)) width, height = img.size x, y = np.random.randint(width-img_width), np.random.randint(height-img_height) # special type image to np array dd = img_to_array(img) ss = dd[y:y+img_height, x:x+img_width] yy = np.expand_dims(ss, axis=0) yield preprocess_input(yy) yield np.array([[[]]])def create_model(weights_path=None, inp_shape=None): # img_input = Input(shape=inp_shape) model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=inp_shape) pretr_layer_outputs = {} for layer in model.layers: pretr_layer_outputs[layer.name] = layer.get_output_at(0) if model.name == 'resnet50': x = pretr_layer_outputs['activation_49'] # we need to skip the very last layer in case of ResNet else: x = model.output # model_pretrained.summary() # x = Conv2D(512, (1, 1), activation='elu', padding='valid', name='Kir_0')(x) # x = BatchNormalization()(x) # x = Conv2D(512, (7, 7), activation='elu', padding='valid', name='Kir_1')(x) # x = Dropout(0.25)(x) # x = Conv2D(256, (1, 1), activation='elu', padding='valid', name='Kir_2')(x) # x = BatchNormalization()(x) # x = Conv2D(1, (1, 1), activation='relu', padding='valid', name='Kir_3')(x) # # predictions = Reshape(target_shape=(1,))(x) x = MaxPool2D(pool_size=(7, 7), padding='same', name='Kir_Pool0')(x) x = BatchNormalization()(x) x = Conv2D(256, (1, 1), activation='elu', padding='valid', name='Kir_0')(x) x = MaxPool2D(pool_size=(3, 3), padding='same', name='Kir_Pool2')(x) # x = BatchNormalization()(x) # x = Conv2D(256, (1, 1), activation='elu', padding='valid', name='Kir_2')(x) # x = BatchNormalization()(x) x = Conv2D(1, (1, 1), activation='relu', padding='valid', name='Kir_3')(x) # predictions = Reshape(target_shape=(1,))(x) model = Model(model.input, x, name='kir_b') # model.summary() # load weights model.load_weights(weights_path) return modelmodel = create_model(weights_path="./cp/43-e054-vl0.62.hdf5", inp_shape=(img_width, img_height, 3) )gen = eval_generator(img_dir="../Sealion/Train")np.set_printoptions(formatter={'float': '{: 0.4f}'.format})# while True:for i in range(30): img_batch = next(gen) if img_batch.size == 0: break prediction = model.predict(img_batch) aaa = prediction.squeeze() print("%d: mean: %s med: %s min: %s max: %s" % (i, np.mean(aaa, (0,1)),np.median(aaa, (0,1)),np.min(aaa, (0,1)),np.max(aaa, (0,1)))) img = img_batch.squeeze() img = (img - np.min(img))/300. # plt.imshow(img); plt.show()