def model_with_inceptionv3(): base_model = InceptionV3(input_shape=(IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS), weights='imagenet', include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) CNN_shared_layer_end = Dropout(0.5)(x) # for age prediction _ = Dense(units=128, activation='relu')(CNN_shared_layer_end) age_output = Dense(units=len(output_mapper['age']), activation='softmax', name='age_output')(_) # # for race prediction _ = Dense(units=128, activation='relu')(CNN_shared_layer_end) race_output = Dense(units=len(output_mapper['race']), activation='softmax', name='race_output')(_) # for emotion prediction _ = Dense(units=128, activation='relu')(CNN_shared_layer_end) emotion_output = Dense(units=len(output_mapper['emotion']), activation='softmax', name='emotion_output')(_) # for gender prediction _ = Dense(units=320, activation='relu' )(CNN_shared_layer_end) gender_output = Dense(units=len(output_mapper['gender']), activation='softmax', name='gender_output')(_) for layer in base_model.layers: layer.trainable = False model = Model(inputs=base_model.input, outputs=[age_output, race_output, emotion_output, gender_output]) model.compile(optimizer='rmsprop', loss={'age_output': 'categorical_crossentropy', 'race_output': 'categorical_crossentropy', 'emotion_output': 'categorical_crossentropy', 'gender_output': 'categorical_crossentropy'}, loss_weights={'age_output': 1.5, 'emotion_output': 1.8, 'race_output': 2., 'gender_output': 1.}, metrics={'age_output': 'accuracy', 'race_output': 'accuracy', 'emotion_output':'accuracy', 'gender_output': 'accuracy'}) # model.summary() p = np.random.permutation(len(df_new_images)) train_up_to = int(len(df_new_images) * TRAIN_TEST_SPLIT) train_idx = p[:train_up_to] valid_idx = p[train_up_to:] batch_size = 64 valid_batch_size = 64 train_gen = get_images(df_new_images, train_idx, for_training=True, batch_size=batch_size) valid_gen = get_images(df_new_images, valid_idx, for_training=True, batch_size=valid_batch_size) model.fit_generator(train_gen, steps_per_epoch=len(train_idx)//batch_size, epochs=20, validation_data=valid_gen, validation_steps=len(valid_idx)//valid_batch_size) for layer in model.layers[:249]: layer.trainable = False for layer in model.layers[249:]: layer.trainable = True model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit_generator(train_gen, steps_per_epoch=len(train_idx)//batch_size, epochs=20, validation_data=valid_gen, validation_steps=len(valid_idx)//valid_batch_size) return model
def run(): # data_link_dict = get_skfold_data(path="../data/imgs/*.jpg") start_time = time.time() # decommisioned because inflight data augmentation solves a lot of these # problems # Use json to load the permanent dictionary that has been Created with open("../data/data_splits.json") as infile: data_link_dict = json.load(infile) EPOCHS = 5 AUGMENTATION = 1 # could do 3 epochs of 10 augmentation or 30 of 1 which # provides more data for plots to work with MINITRAINS = 30 DO = 0.55 # drop out # for Adam inital LR of 0.0001 is a good starting point # for SGD initial LR of 0.001 is a good starting point LR = 0.00025 DECAY = 0.5e-6 L2_REG = 0.05 OPTIMIZER = Adam(lr=LR, decay=DECAY) # OPTIMIZER = SGD(lr=LR, momentum=0.9, nesterov=True) NB_IV3_LAYERS_TO_FREEZE = 172 MODEL_ID = 'v2_5z' plot_file = "model_{:}.png".format(MODEL_ID) weights_file = "weights/model_{:}_weights.h5".format(MODEL_ID) history_file = "histories/history_{:}.json".format(MODEL_ID) # # user parameters for LoaderBot v1.0 # # Parameters for Generators # params = {'dim': (299, 299), # 'batch_size': 256, # 'n_classes': 128, # 'n_channels': 3, # 'shuffle': False} # These parameters are for LoaderBot v2.0 # Parameters for Generators params = { 'batch_size': 128, 'random_pics': 1, 'percent_random': 0.0, 'shuffle': True } # # Parameters for Generators test_params = {'batch_size': 128, 'shuffle': False} # Datasets X_train_img_paths = data_link_dict["X_test_1"] y_train = data_link_dict["y_test_1"] X_test_img_paths = data_link_dict["X_test_2"] y_test = data_link_dict["y_test_2"] # Generators training_generator = LoaderBot(X_train_img_paths, y_train, **params) validation_generator = LoaderBot(X_test_img_paths, y_test, **test_params) # setup model base_model = InceptionV3( weights='imagenet', include_top=False) #include_top=False excludes final FC layer model = regular_brian_layers(base_model, 128, DO, l1_reg=0.0005) # print(model.summary()) # mini-train 1, like normal # transfer learning setup_to_transfer_learn(model, base_model, OPTIMIZER) history = {} # preinitialize history log for mt in range(MINITRAINS): temp = mt + 1 if temp == 1: # Run model new_history = model.fit_generator( generator=training_generator, validation_data=validation_generator, epochs=EPOCHS, use_multiprocessing=False) history["acc"] = new_history.history["acc"] history["val_acc"] = new_history.history["val_acc"] history["loss"] = new_history.history["loss"] history["val_loss"] = new_history.history["val_loss"] else: temp_lr = LR / (10.0**(mt / 5 - (mt // 7.5))) print("\n\nLearning rate for mini-train: {:2.8f}\n\n".format( temp_lr)) # mini-train 2 OPTIMIZER = Adam(lr=temp_lr, decay=0.0) # try to fine tune some of the InceptionV3 layers also thaw_count = int(2.5 * temp) if thaw_count > 50: thaw_count = 50 thaw_count = NB_IV3_LAYERS_TO_FREEZE - thaw_count setup_to_finetune(model, thaw_count, OPTIMIZER, L2_REG) model = activate_regularization(model) print("\n\n Starting epoch {:}\n\n".format(EPOCHS * mt + 1)) # Run model new_history = model.fit_generator( generator=training_generator, validation_data=validation_generator, epochs=EPOCHS, use_multiprocessing=False) # save the weights in case we want to predict on them later model.save(weights_file) history["acc"] += new_history.history["acc"] history["val_acc"] += new_history.history["val_acc"] history["loss"] += new_history.history["loss"] history["val_loss"] += new_history.history["val_loss"] # seems to be prepending a 0 to the list so ignore that history["acc"] = history["acc"] history["val_acc"] = history["val_acc"] history["loss"] = history["loss"] history["val_loss"] = history["val_loss"] temp_hist = temp_clean_history(history) plot_hist(temp_hist, plot_file, epochs=len(history["acc"]), sprint=True) # try to save the history so models can be more easily compared and Also # to better log results if going back is needed with open(history_file, "w") as outfile: json.dump(history, outfile) print("\n\n\n\nCompleted in {:6.2f} hrs".format( ((time.time() - start_time)) / 3600)) # convert to hours
#Source: https://www.pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras/ from keras.applications import InceptionV3 from keras.applications import imagenet_utils from keras.applications.inception_v3 import preprocess_input from keras.preprocessing.image import img_to_array from keras.preprocessing.image import load_img import numpy as np global model model = InceptionV3() def reshapeImage(img): preprocess = preprocess_input image = load_img(img, target_size=(299, 299)) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = preprocess(image) return image def predict_image(image): preds = model.predict(image) P = imagenet_utils.decode_predictions(preds) p = [] for (i, (_, label, prob)) in enumerate(P[0]): p.append({'label': label, 'prob': prob * 100}) return p
parser = argparse.ArgumentParser(description="args parser") parser.add_argument("-modelzoo_file") parser.add_argument("-conv_layer_idx", type=int) parser.add_argument("-filter_idx", type=int) # optional parser.add_argument("-num_of_filters", type=int) parser.add_argument("-input_image_path") args = parser.parse_args() class LucidInceptionV3(Model): # model_path = 'keras_inception_v3_frozen.pb.modelzoo' model_path = args.modelzoo_file image_shape = [299, 299, 3] image_value_range = (0, 1) input_name = 'input_1' keras_model = InceptionV3(weights='imagenet', input_shape=(299, 299, 3)) inceptionv3 = LucidInceptionV3() inceptionv3.load_graphdef() # Image Processing img_path = args.input_image_path img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) x = preprocess_input(x) y = image.img_to_array(img) y = np.expand_dims(y, axis=0) y = preprocess_input(y) layer_idx = args.conv_layer_idx for i in range(len(keras_model.layers)): if keras_model.layers[i].name == "conv2d_{}".format(layer_idx):
# print(x_test.shape) # print(y_train.shape) # print(y_test.shape) x_train = x_train.astype('float32') / 255 x_test = x_test.astype('float') / 255 y_train = to_categorical(y_train) y_test = to_categorical(y_test) # print(y_train.shape) # print(y_test.shape) conv_base = InceptionV3(weights='imagenet', include_top=False, input_shape=(112, 112, 3)) # conv_base.summary() model = models.Sequential() model.add(conv_base) model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(10, activation='sigmoid')) # model.summary() model.compile(optimizer='adadelta', loss='binary_crossentropy', metrics=['accuracy'])
def InceptionV3(include_top, image_input, **kwargs): model = InceptionV3(input_tensor=image_input, include_top=include_top, weights='imagenet') return model
from keras.applications import VGG16 from keras.applications import InceptionV3 from keras.applications import ResNet50 from keras.utils import plot_model vgg = VGG16() inception = InceptionV3() resnet = ResNet50() plot_model(vgg, to_file="output/visualizations/VGG16.png", show_shapes=True) plot_model(inception, to_file="output/visualizations/InceptionV3.png", show_shapes=True) plot_model(resnet, to_file="output/visualizations/ResNet50.png", show_shapes=True)
'_image_tags.txt', 'w', encoding='utf8') deep_learning_feature_file_name = 'feature_data/' + dataset_name + '_' + model_name + '_image_tag_feature.npy' if model_name == 'VGG16': model = VGG16(weights='imagenet', include_top=True) im_size = 224 elif model_name == 'VGG19': model = VGG19(weights='imagenet', include_top=True) im_size = 224 elif model_name == 'ResNet50': model = ResNet50(weights='imagenet', include_top=True) im_size = 224 elif model_name == 'InceptionV3': model = InceptionV3(weights='imagenet', include_top=True) im_size = 299 elif model_name == 'Xception': model = Xception(weights='imagenet', include_top=True) im_size = 299 #print(model.summary()) for im in os.listdir(im_path): print(im) try: img = Image.load_img(im_path + im, target_size=(im_size, im_size)) except: print(im_path + im) continue x = Image.img_to_array(img)
from keras import applications, metrics, layers, models, regularizers, optimizers from keras.applications import InceptionV3, Xception from keras.models import * from keras.layers import * import numpy as np import time, cv2, os from os.path import expanduser # to get relative paths without using the user name home = expanduser('~') IMAGE_SIZE = (139, 139) NUM_CLASSES = 2 base_model = InceptionV3(include_top=False, weights=None, input_tensor=None, input_shape=(IMAGE_SIZE[0],IMAGE_SIZE[1],3)) x = base_model.output x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dropout(0.5)(x) predictions = Dense(NUM_CLASSES, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) model.load_weights(home + '/ownCloud/imagine_weights/screw_detector/pretrained_models/weights_inceptionv3/weights.15.h5', by_name=True) class_order = ['none', 'screw'] imgs = os.listdir(home + '/ownCloud/imagine_images/screw_data/test1/non_screw/') tp,tn,fp,fn = 0,0,0,0 for name in imgs:
def load_model(self): self.model = InceptionV3() self.model._make_predict_function()
def __init__(self, input_shape, weights): self.feature_extractor = InceptionV3(input_shape=input_shape, include_top=False, weights=weights)
def inceptionv3(): model = InceptionV3(weights="imagenet") graph = tf.get_default_graph() return model, graph
def main(): if not os.path.exists("./dataset/Specgrams"): print("no data detected, exit.") exit() if not os.path.exists("./dataset/Transferred"): os.makedirs("./dataset/Transferred") if not os.path.exists("./dataset/Transferred/icp"): os.makedirs("./dataset/Transferred/icp") if not os.path.exists("./dataset/Transferred/icp/0"): os.makedirs("./dataset/Transferred/icp/0") if not os.path.exists("./dataset/Transferred/icp/1"): os.makedirs("./dataset/Transferred/icp/1") if not os.path.exists("./dataset/Transferred/icp/-1"): os.makedirs("./dataset/Transferred/icp/-1") if not os.path.exists("./dataset/Transferred/vgg"): os.makedirs("./dataset/Transferred/vgg") if not os.path.exists("./dataset/Transferred/vgg/0"): os.makedirs("./dataset/Transferred/vgg/0") if not os.path.exists("./dataset/Transferred/vgg/1"): os.makedirs("./dataset/Transferred/vgg/1") if not os.path.exists("./dataset/Transferred/vgg/-1"): os.makedirs("./dataset/Transferred/vgg/-1") dirs = "./dataset/Specgrams" if X == 1: x = "Inception_V3" else: x = "VGG_16" if x == "Inception_V3": m = InceptionV3(weights='imagenet', include_top=False, input_shape=(299, 299, 3), pooling='max') elif x == "VGG_16": m = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling='max') else: m = InceptionV3(weights='imagenet', include_top=False, input_shape=(299, 299, 3), pooling='max') # to save RAM, please create one model. # get features... for i in range(-1, 2): for val in os.listdir("{}/{}".format(dirs, i)): # if ".bmp" in val: if ".png" in val: if x == "Inception_V3": img = image.load_img("{}/{}/{}".format(dirs, i, val), target_size=(299, 299), interpolation="bilinear") else: img = image.load_img("{}/{}/{}".format(dirs, i, val), target_size=(224, 224), interpolation="bilinear") data = image.img_to_array(img) data = np.expand_dims(data, axis=0) data = preprocess_input(data) feature = m.predict(data) # save result if x == "Inception_V3": np.save( "./dataset/Transferred/icp/{}/{}".format(i, val[:-4]), feature) elif x == "VGG_16": np.save( "./dataset/Transferred/vgg/{}/{}".format(i, val[:-4]), feature) print("Function finished.")
from DataPreProcessing import X, y, IMG_SIZE, x_test, y_test from keras import models from keras import layers from keras.optimizers import SGD from keras.applications import InceptionV3 Incp_con = InceptionV3(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3)) model = models.Sequential() model.add(Incp_con) model.add(layers.Flatten()) model.add(layers.Dense(1024, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(7, activation='softmax')) sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=['accuracy'] ) model.fit(X, y, epochs=200, validation_split=0.2, batch_size=32) model.save("model.h5") model.evaluate(x_test, y_test)
save_dir = os.path.join(os.getcwd(), 'saved_models') model_name = 'ResNet_10class_lr.h5' weight_name = 'ResNet_10class_weight_lr.h5' model_path = os.path.join(save_dir, model_name) weight_path = os.path.join(save_dir, weight_name) x_train, y_train, x_test, y_test, dictionary = Dataset_10.get_dataset() x_train_resized = Dataset_10.resize_imgs(x_train, 150) x_test_resized = Dataset_10.resize_imgs(x_test, 150) y_train = to_categorical(y_train, num_classes=10) y_test = to_categorical(y_test, num_classes=10) # model = load_model(model_path) model = InceptionV3(include_top=True, weights=None, classes=10) opt = Adam(lr=2e-5) model.compile(optimizer=opt, loss=losses.categorical_crossentropy, metrics=[metrics.categorical_accuracy]) model.fit(x_train_resized, y_train, epochs=30, batch_size=15) model.save(model_path) model.save_weights(weight_path) score1 = model.evaluate(x_train_resized, y_train, batch_size=10) score2 = model.evaluate(x_test_resized, y_test, batch_size=10) print("Score data training : ", score1) print("Score data testing : ", score2)
# initialize the validation/testing data augmentation object (which # we'll be adding mean subtraction to) valAug = ImageDataGenerator() # define the ImageNet mean subtraction (in RGB order) and set the # the mean subtraction value for each of the data augmentation # objects mean = np.array([123.68, 116.779, 103.939], dtype="float32") trainAug.mean = mean valAug.mean = mean # load the ResNet-50 network, ensuring the head FC layer sets are left # off baseModel = InceptionV3(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3))) # construct the head of the model that will be placed on top of the # the base model headModel = baseModel.output headModel = AveragePooling2D(pool_size=(5, 5))(headModel) headModel = Flatten(name="flatten")(headModel) headModel = Dense(512, activation="relu")(headModel) headModel = Dropout(0.5)(headModel) headModel = Dense(len(lb.classes_), activation="softmax")(headModel) # place the head FC model on top of the base model (this will become # the actual model we will train) model = Model(inputs=baseModel.input, outputs=headModel)
def SSD(input_shape, num_classes): """SSD512 architecture. # Arguments input_shape: Shape of the input image, expected to be either (512, 512, 3) or (3, 512, 512)(not tested). num_classes: Number of classes including background. # References https://arxiv.org/abs/1512.02325 """ # Block 1 input_shape = (input_shape[1], input_shape[0], 3) input = Input(input_shape) inceptionV3 = InceptionV3(input_shape=input_shape, include_top=False, weights='imagenet') FeatureExtractor = Model(inputs=inceptionV3.input, outputs=inceptionV3.get_layer('mixed2').output) pool3 = FeatureExtractor(input) conv4_0 = Conv2DTranspose(512, (4, 4), name='conv4_inceptionv3', activation='relu', border_mode='valid')(pool3) #for inceptionV3 # Block 4 conv4_1 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')(conv4_0) conv4_2 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')(conv4_1) conv4_3 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')(conv4_2) pool4 = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='pool4')(conv4_3) # Block 5 conv5_1 = Conv2D(512, (3, 3), name='conv5_1', padding='same', activation='relu')(pool4) conv5_2 = Conv2D(512, (3, 3), name='conv5_2', padding='same', activation='relu')(conv5_1) conv5_3 = Conv2D(512, (3, 3), name='conv5_3', padding='same', activation='relu')(conv5_2) pool5 = MaxPooling2D(name='pool5', pool_size=(3, 3), strides=(1, 1), padding='same')(conv5_3) # FC6 fc6 = Conv2D(1024, (3, 3), name='fc6', dilation_rate=(6, 6), padding='same', activation='relu')(pool5) #5 # x = Dropout(0.5, name='drop6')(x) # FC7 fc7 = Conv2D(1024, (1, 1), name='fc7', padding='same', activation='relu')(fc6) # x = Dropout(0.5, name='drop7')(x) # Block 6 conv6_1 = Conv2D(256, (1, 1), name='conv6_1', padding='same', activation='relu')(fc7) conv6_2 = Conv2D(512, (3, 3), name='conv6_2', strides=(2, 2), padding='same', activation='relu')(conv6_1) # Block 7 conv7_1 = Conv2D(128, (1, 1), name='conv7_1', padding='same', activation='relu')(conv6_2) conv7_1z = ZeroPadding2D(name='conv7_1z')(conv7_1) conv7_2 = Conv2D(256, (3, 3), name='conv7_2', padding='valid', strides=(2, 2), activation='relu')(conv7_1z) # Block 8 conv8_1 = Conv2D(128, (1, 1), name='conv8_1', padding='same', activation='relu')(conv7_2) conv8_2 = Conv2D(256, (3, 3), name='conv8_2', padding='same', strides=(2, 2), activation='relu')(conv8_1) # Last Pool pool6 = GlobalAveragePooling2D(name='pool6')(conv8_2) #8_2 # Prediction from conv4_3 num_priors = 3 img_size = (input_shape[1], input_shape[0]) name = 'conv4_3_norm_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) conv4_3_norm = Normalize(20, name='conv4_3_norm')(conv4_3) #4_3 conv4_3_norm_mbox_loc = Conv2D(num_priors * 4, (3, 3), name='conv4_3_norm_mbox_loc', padding='same')(conv4_3_norm) conv4_3_norm_mbox_loc_flat = Flatten( name='conv4_3_norm_mbox_loc_flat')(conv4_3_norm_mbox_loc) conv4_3_norm_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), name=name, padding='same')(conv4_3_norm) conv4_3_norm_mbox_conf_flat = Flatten( name='conv4_3_norm_mbox_conf_flat')(conv4_3_norm_mbox_conf) conv4_3_norm_mbox_priorbox = PriorBox(img_size, 30.0, name='conv4_3_norm_mbox_priorbox', aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2])(conv4_3_norm) # Prediction from fc7 num_priors = 6 name = 'fc7_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) fc7_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), padding='same', name=name)(fc7) fc7_mbox_conf_flat = Flatten(name='fc7_mbox_conf_flat')(fc7_mbox_conf) fc7_mbox_loc = Conv2D(num_priors * 4, (3, 3), name='fc7_mbox_loc', padding='same')(fc7) fc7_mbox_loc_flat = Flatten(name='fc7_mbox_loc_flat')(fc7_mbox_loc) fc7_mbox_priorbox = PriorBox(img_size, 60.0, name='fc7_mbox_priorbox', max_size=114.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2])(fc7) # Prediction from conv6_2 num_priors = 6 name = 'conv6_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) conv6_2_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), padding='same', name=name)(conv6_2) conv6_2_mbox_conf_flat = Flatten( name='conv6_2_mbox_conf_flat')(conv6_2_mbox_conf) conv6_2_mbox_loc = Conv2D(num_priors * 4, ( 3, 3, ), name='conv6_2_mbox_loc', padding='same')(conv6_2) conv6_2_mbox_loc_flat = Flatten( name='conv6_2_mbox_loc_flat')(conv6_2_mbox_loc) conv6_2_mbox_priorbox = PriorBox(img_size, 114.0, max_size=168.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv6_2_mbox_priorbox')(conv6_2) # Prediction from conv7_2 num_priors = 6 name = 'conv7_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) conv7_2_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), padding='same', name=name)(conv7_2) conv7_2_mbox_conf_flat = Flatten( name='conv7_2_mbox_conf_flat')(conv7_2_mbox_conf) conv7_2_mbox_loc = Conv2D(num_priors * 4, (3, 3), padding='same', name='conv7_2_mbox_loc')(conv7_2) conv7_2_mbox_loc_flat = Flatten( name='conv7_2_mbox_loc_flat')(conv7_2_mbox_loc) conv7_2_mbox_priorbox = PriorBox(img_size, 168.0, max_size=222.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv7_2_mbox_priorbox')(conv7_2) # Prediction from conv8_2 num_priors = 6 name = 'conv8_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) conv8_2_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), padding='same', name=name)(conv8_2) conv8_2_mbox_conf_flat = Flatten( name='conv8_2_mbox_conf_flat')(conv8_2_mbox_conf) conv8_2_mbox_loc = Conv2D(num_priors * 4, (3, 3), padding='same', name='conv8_2_mbox_loc')(conv8_2) conv8_2_mbox_loc_flat = Flatten( name='conv8_2_mbox_loc_flat')(conv8_2_mbox_loc) conv8_2_mbox_priorbox = PriorBox(img_size, 222.0, max_size=276.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv8_2_mbox_priorbox')(conv8_2) # Prediction from pool6 num_priors = 6 name = 'pool6_mbox_conf_flat' if num_classes != 21: name += '_{}'.format(num_classes) if K.image_dim_ordering() == 'tf': target_shape = (1, 1, 256) else: target_shape = (256, 1, 1) pool6_mbox_loc_flat = Dense(num_priors * 4, name='pool6_mbox_loc_flat')(pool6) pool6_mbox_conf_flat = Dense(num_priors * num_classes, name=name)(pool6) pool6_reshaped = Reshape(target_shape, name='pool6_reshaped')(pool6) pool6_mbox_priorbox = PriorBox(img_size, 276.0, max_size=330.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='pool6_mbox_priorbox')(pool6_reshaped) # Gather all predictions mbox_loc = concatenate([ conv4_3_norm_mbox_loc_flat, fc7_mbox_loc_flat, conv6_2_mbox_loc_flat, conv7_2_mbox_loc_flat, conv8_2_mbox_loc_flat, pool6_mbox_loc_flat ], axis=1, name='mbox_loc') mbox_conf = concatenate([ conv4_3_norm_mbox_conf_flat, fc7_mbox_conf_flat, conv6_2_mbox_conf_flat, conv7_2_mbox_conf_flat, conv8_2_mbox_conf_flat, pool6_mbox_conf_flat ], axis=1, name='mbox_conf') mbox_priorbox = concatenate([ conv4_3_norm_mbox_priorbox, fc7_mbox_priorbox, conv6_2_mbox_priorbox, conv7_2_mbox_priorbox, conv8_2_mbox_priorbox, pool6_mbox_priorbox ], axis=1, name='mbox_priorbox') if hasattr(mbox_loc, '_keras_shape'): num_boxes = mbox_loc._keras_shape[-1] // 4 elif hasattr(mbox_loc, 'int_shape'): num_boxes = K.int_shape(mbox_loc)[-1] // 4 mbox_loc = Reshape((num_boxes, 4), name='mbox_loc_final')(mbox_loc) mbox_conf = Reshape((num_boxes, num_classes), name='mbox_conf_logits')(mbox_conf) mbox_conf = Activation('softmax', name='mbox_conf_final')(mbox_conf) predictions = concatenate([mbox_loc, mbox_conf, mbox_priorbox], axis=2, name='predictions') model = Model(input, outputs=predictions) return model
url_list = get_images_urls(label_list) print(url_list) from keras.applications.inception_v3 import preprocess_input from keras.preprocessing.image import img_to_array, load_img import numpy as np import os from pyspark.sql.types import StringType from sparkdl import KerasImageFileTransformer from keras.applications import InceptionV3 # Load inception v3 (Best Image Classifier) model = InceptionV3(weights="imagenet") # Save the model model.save('/tmp/model-full.h5') # Parameters SIZE = (299, 299) # Size accepted by Inception model IMAGES_PATH = 'datasets/image_classifier/test/' # Images Path MODEL = '/tmp/model-full-tmp.h5' # Model Path # Image Preprocessing def preprocess_keras_inceptionV3(uri): image = img_to_array(load_img(uri, target_size=SIZE)) image = np.expand_dims(image, axis=0) return preprocess_input(image)
def run(): data_link_dict = get_skfold_data(path="../data/imgs/*.jpg") start_time = time.time() # decommisioned because inflight data augmentation solves a lot of these # problems # # Use json to load the permanent dictionary that has been Created # with open("../data/data_splits.json") as infile: # data_link_dict = json.load(infile) EPOCHS = 20 AUGMENTATION = 1 # could do 3 epochs of 10 augmentation or 30 of 1 which # provides more data for plots to work with LR = 0.0005 NB_IV3_LAYERS_TO_FREEZE = 172 # Parameters for Generators params = { 'dim': (299, 299), 'batch_size': 256, 'n_classes': 128, 'n_channels': 3, 'augmentation': AUGMENTATION, 'shuffle': True } # Parameters for Generators test_params = { 'dim': (299, 299), 'batch_size': 256, 'n_classes': 128, 'n_channels': 3, 'augmentation': 1, 'augment': False, 'shuffle': True } # Datasets X_train_img_paths = data_link_dict["X_test_1"] y_train = data_link_dict["y_test_1"] X_test_img_paths = data_link_dict["X_test_2"] y_test = data_link_dict["y_test_2"] # Generators training_generator = LoaderBot(X_train_img_paths, y_train, **params) validation_generator = LoaderBot(X_test_img_paths, y_test, **test_params) # setup model base_model = InceptionV3( weights='imagenet', include_top=False) #include_top=False excludes final FC layer model = add_brian_layers(base_model, 128, 0.55) # mini-train 1, like normal # transfer learning setup_to_transfer_learn(model, base_model, lr=LR) # Run model history_t1 = model.fit_generator(generator=training_generator, validation_data=validation_generator, epochs=EPOCHS, use_multiprocessing=False) # mini-train 2 # try to fine tune some of the InceptionV3 layers also setup_to_finetune(model, NB_IV3_LAYERS_TO_FREEZE - 3, lr=LR / 2.0) # Run model history_t2 = model.fit_generator(generator=training_generator, validation_data=validation_generator, epochs=EPOCHS, use_multiprocessing=False) # mini-train 3 # try to fine tune some of the InceptionV3 layers also setup_to_finetune(model, NB_IV3_LAYERS_TO_FREEZE - 6, lr=LR / 4.0) # Run model history_t3 = model.fit_generator(generator=training_generator, validation_data=validation_generator, epochs=EPOCHS, use_multiprocessing=False) # mini-train 4 # try to fine tune some of the InceptionV3 layers also setup_to_finetune(model, NB_IV3_LAYERS_TO_FREEZE - 9, lr=LR / 8.0) # Run model history_t4 = model.fit_generator(generator=training_generator, validation_data=validation_generator, epochs=EPOCHS, use_multiprocessing=False) model.save("model_v2_0d_weights.h5") history_tl = history_t1.history history_tl["acc"] += history_t2.history["acc"] history_tl["val_acc"] += history_t2.history["val_acc"] history_tl["loss"] += history_t2.history["loss"] history_tl["val_loss"] += history_t2.history["val_loss"] # history_tl = history_t1.history history_tl["acc"] += history_t3.history["acc"] history_tl["val_acc"] += history_t3.history["val_acc"] history_tl["loss"] += history_t3.history["loss"] history_tl["val_loss"] += history_t3.history["val_loss"] history_tl = history_t1.history history_tl["acc"] += history_t4.history["acc"] history_tl["val_acc"] += history_t4.history["val_acc"] history_tl["loss"] += history_t4.history["loss"] history_tl["val_loss"] += history_t4.history["val_loss"] plot_hist(history_tl, "model_v2_0d.png", epochs=len(history_tl["acc"])) print("\n\n\n\nCompleted in {:6.2f} hrs".format( ((time.time() - start_time)) / 3600)) # convert to hours
img_normal = plt.imread(f'{TRAIN_DIR}/NORMAL/IM-0117-0001.jpeg') # Pneumonia img_penumonia = plt.imread(f'{TRAIN_DIR}/PNEUMONIA/person1_bacteria_2.jpeg') # Plot Configuration plt.figure(figsize=(12, 5)) plt.subplot(1,3,1).set_title('Label: NORMAL') plt.imshow(img_normal, cmap='gray') plt.subplot(1,3,2).set_title('Label : PNEUMONIA') plt.imshow(img_penumonia, cmap='gray') plt.tight_layout() """# **Customizing the Inceptionv3 model**""" base_model=InceptionV3(weights='imagenet',include_top=False, input_shape=(224,224,3)) #imports the inceptionv3 model # and slices off the top layer which is the one that classifies objects into various classes (we don't want this layer) # Setting up the pre-trained weights of the inceptionv3 model as non-trainable for layer in base_model.layers: layer.trainable=False # Adding new layers on top of the base model x=base_model.output x=GlobalAveragePooling2D()(x) x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results x=Dense(1024,activation='relu')(x) #dense layer 2 x=Dense(512,activation='relu')(x) #dense layer 3 preds=Dense(2,activation='softmax')(x) #final layer with softmax activation - here, we have only 2 classes (Pneumonia & Normal) # Specify the inputs
def get_model(model_name, dataset, dropout, fc_layers=(1024, 1024), acf='relu', pred_acf='softmax'): #ResNet50 | DenseNet121 | MobileNetV2 | VGG19 | InceptionV3 print("Getting model... ", model_name) weights = 'imagenet' if model_name == "ResNet50": base_model = ResNet50(weights=weights, include_top=False, input_shape=dataset.image_shape) elif model_name == "DenseNet121": base_model = DenseNet121(weights=weights, include_top=False, input_shape=dataset.image_shape) #elif model_name == "MobileNetV2": # base_model = MobileNetV2( # weights=weights, include_top=False, input_shape=dataset.image_shape) elif model_name == "VGG19": base_model = VGG19(weights=weights, include_top=False, input_shape=dataset.image_shape) elif model_name == "VGG16": base_model = VGG16(weights=weights, include_top=False, input_shape=dataset.image_shape) elif model_name == "VGG16fix": base_model = vggfix.VGG16_fix(weights=weights, include_top=False, input_shape=dataset.image_shape) elif model_name == "AlexNetfix": base_model = AlexNetfix.AlexNet_fix(weights=None, include_top=False, input_shape=dataset.image_shape) elif model_name == "AlexNet": base_model = AlexNetfix.AlexNet(weights=None, include_top=False, input_shape=dataset.image_shape) elif model_name == "VGGsmallfix": base_model = vggfix.VGG_small_fix(include_top=False, input_shape=dataset.image_shape) elif model_name == "VGG7": tmp_base_model = VGG16(weights=weights, include_top=False, input_shape=dataset.image_shape) #tmp_base_model.layers = tmp_base_model.layers[:-5] #tmp_base_model.outputs = [tmp_base_model.layers[-1].output] base_model = Model(inputs=tmp_base_model.input, outputs=tmp_base_model.layers[6].output) elif model_name == "InceptionV3": base_model = InceptionV3(weights=weights, include_top=False, input_shape=dataset.image_shape) else: print("Wrong input argument, use either: ResNet50 or DenseNet121...") FC_LAYERS = fc_layers finetuned_model = build_finetuned_model(base_model, dropout=dropout, fc_layers=FC_LAYERS, num_classes=dataset.num_classes, acf=acf, pred_acf=pred_acf) finetuned_model.summary() return finetuned_model
def run(): # data_link_dict = get_skfold_data(path="../data/imgs/*.jpg") start_time = time.time() EPOCHS = 5 MINITRAINS = 30 DO = 0.625 # drop out SPRINT = True # is this run a full train or a sprint # for Adam inital LR of 0.0001 is a good starting point # for SGD initial LR of 0.001 is a good starting point LR = 0.00025 DECAY = 0.5e-6 L2_REG = 0.125 OPTIMIZER = Adam(lr=LR, decay=DECAY) # OPTIMIZER = SGD(lr=LR, momentum=0.9, nesterov=True) NB_IV3_LAYERS_TO_FREEZE = 172 MODEL_ID = 'v2_7m' plot_file = "model_{:}.png".format(MODEL_ID) weights_file = "weights/model_{:}_weights.h5".format(MODEL_ID) history_file = "histories/history_{:}.json".format(MODEL_ID) # # user parameters for LoaderBot v1.0 # # Parameters for Generators # params = {'dim': (299, 299), # 'batch_size': 256, # 'n_classes': 128, # 'n_channels': 3, # 'shuffle': False} # These parameters are for LoaderBot v2.0 # Parameters for Generators params = { 'batch_size': 128, 'augment': True, 'augment_odds': 1.0, 'shuffle': True } # # Parameters for Generators test_params = {'batch_size': 128, 'augment': False, 'shuffle': False} # Datasets data = get_data("../data/metadata_splits.json") print(data["X_train"][:5]) # Generators training_generator = LoaderBot(data["X_train"], data["y_train"], **params) validation_generator = LoaderBot(data["X_test"], data["y_test"], **test_params) # setup model base_model = InceptionV3( weights='imagenet', include_top=False) # include_top=False excludes final FC layer model = neo_brian_layers(base_model, 128, DO) # print(model.summary()) # model.load_weights("weights/model_v2_7g_weights.h5") # mini-train 1, like normal # transfer learning setup_to_transfer_learn(model, base_model, OPTIMIZER) history = {} # preinitialize history log for mt in range(MINITRAINS): temp = mt + 1 if temp == 1: # Run model new_history = model.fit_generator( generator=training_generator, validation_data=validation_generator, epochs=EPOCHS, use_multiprocessing=False) history["acc"] = new_history.history["acc"] history["val_acc"] = new_history.history["val_acc"] history["loss"] = new_history.history["loss"] history["val_loss"] = new_history.history["val_loss"] else: # params["augment_odds"] += 0.05 # if params["augment_odds"] > 1.0: # params["augment_odds"] = 1.0 # training_generator = LoaderBot(data["X_train"], data["y_train"], **params) temp_lr = LR / (10.0**(mt / 5 - (mt // 7.5))) # temp_lr = LR / 1.5**temp # mini-train 2 OPTIMIZER = Adam(lr=temp_lr, decay=0.0) # try to fine tune some of the InceptionV3 layers also thaw_count = int(2.5 * temp) if thaw_count > 10: thaw_count = 10 thaw_count = NB_IV3_LAYERS_TO_FREEZE - thaw_count setup_to_finetune(model, thaw_count, OPTIMIZER, L2_REG) model = activate_regularization(model) print("\n\n Starting epoch {:}\n\n".format(EPOCHS * mt + 1)) print( "Learning rate for mini-train: {:2.8f} augmentation odds:{:2.2f}\n\n" .format(temp_lr, params["augment_odds"])) # Run model new_history = model.fit_generator( generator=training_generator, validation_data=validation_generator, epochs=EPOCHS, use_multiprocessing=False) # save the weights in case we want to predict on them later model.save(weights_file) history["acc"] += new_history.history["acc"] history["val_acc"] += new_history.history["val_acc"] history["loss"] += new_history.history["loss"] history["val_loss"] += new_history.history["val_loss"] # seems to be prepending a 0 to the list so ignore that history["acc"] = history["acc"] history["val_acc"] = history["val_acc"] history["loss"] = history["loss"] history["val_loss"] = history["val_loss"] plot_hist(history, plot_file, epochs=len(history["acc"]), sprint=SPRINT) # try to save the history so models can be more easily compared and Also # to better log results if going back is needed with open(history_file, "w") as outfile: history = clean_history(history) # clean up zero padding, if it exists json.dump(history, outfile) print("\n\n\n\nCompleted in {:6.2f} hrs".format( ((time.time() - start_time)) / 3600)) # convert to hours
from keras.applications import VGG16, VGG19, Xception, InceptionV3, ResNet50, MobileNet from keras.layers import Dense, Flatten from keras.models import Model, Sequential Xception(), InceptionV3(), ResNet50(), MobileNet() conv_base = VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3)) #conv_base = VGG16() # default input_shape=(224,224,3) , default include_top=True """ 방법 1. x = conv_base.output x = Flatten()(x) x = Dense(256 ,activation="relu")(x) x = Dense(1, activation='sigmoid')(x) conv_base = Model(conv_base.input, x) 방법 2. model = models.Sequential() model.add(conv_base) model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu)) model.add(layers.Dense(1, activation='sigmoid')) """ model = Sequential() model.add(conv_base) model.add(Flatten()) # conv_base가 이미 Flatten() 상태였다면 사용시 Error 생길 수 있다 model.add(Dense(256, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.summary()
import os # remove tensorflow warnings os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' from keras.applications import inception_v3, InceptionV3, imagenet_utils from keras.preprocessing.image import load_img, img_to_array from matplotlib.offsetbox import OffsetImage, AnnotationBbox import matplotlib.pyplot as plt from github import Github import numpy as np import os, uuid # initialize inception model model = InceptionV3(weights='imagenet') def get_inception_vector(image_path): ''' @arg image_path str: the path to an image @returns numpy.ndarray: an array containing the inception model weights for the input image ''' # VGG16, VGG19, and ResNet take 224×224 images; InceptionV3 and Xception take 299×299 inputs img = load_img(image_path, target_size=(299, 299)) # convert the image to an array arr = img_to_array(img) # scale the array to fit the model arr = inception_v3.preprocess_input(arr) # complete forward pass through model return model.predict(np.array([arr])).squeeze()
parser.add_argument('--verbose', type=int, default=2) parser.add_argument('--with_aux', type=str2bool, nargs='?', const=True, default=True) args = parser.parse_args() return args if __name__ == '__main__': args = parse_args() # init new inception3 model inception = InceptionV3(include_top=False, input_shape=(299, 299, 3), weights=None, pooling=None) output = inception.layers[-1].output # add last layers net = AveragePooling2D( pool_size=(8, 8), # K.int_shape(output), strides=(2, 2), padding='valid', data_format='channels_last', name='pool')(output) net = Dropout(rate=0.2, name='dropout')(net) net = Flatten(data_format='channels_last', name='flatten')(net) logits = Dense(NUM_CLASSES, activation=None, name="logits")(net) predictions = Softmax(name='predictions')(logits)
def transfer_learning_model(train_x, train_y, val_x, val_y, test_x, test_y, num_class, epoch, batch_size, model_type, reshape_size, l1_weight, l2_weight): print(train_x.shape) print(type(train_x)) print('\n') print(train_x[0].shape) # change label into one hot train_y = keras.utils.to_categorical(train_y, num_classes=num_class) val_y = keras.utils.to_categorical(val_y, num_classes=num_class) #test_y = keras.utils.to_categorical(test_y, num_classes=num_class) print(test_y) if model_type == 'vgg16': pre_trained = VGG16( weights='imagenet', include_top=False, # True if we want to add Fully Connected Layer at the Last (False) input_shape=reshape_size + (3,) ) pre_trained.trainable = False # False if we want to freeze the weight elif model_type == 'vgg19': pre_trained = VGG19( weights='imagenet', include_top=False, input_shape=reshape_size+ (3,) ) elif model_type == 'resnet101': pre_trained = ResNet101( weights='imagenet', include_top = False input_shape = reshape_size + (3,) ) elif model_type == 'resnet50': pre_trained = ResNet50( weights='imagenet' include_top = False, input_shape = reshape_size + (3,) ) elif model_type == 'xception': pre_trained = Xception( weights='imagenet', include_top=False, input_shape=reshape_size + (3,) ) elif model_type == 'inception_v3': pre_trained = InceptionV3( weights='imagenet', include_top=False, input_shape=reshape_size + (3,) ) elif model_type == 'mobilenet': pre_trained = MobileNet( weights='imagenet', include_top=False, input_shape=reshape_size + (3,) ) #pre_trained.summary() # Add Fine-Tuning Layers finetune_model = models.Sequential() finetune_model.add(pre_trained) if model_type == 'resnet50': pass else: finetune_model.add(layers.Flatten()) finetune_model.add(layers.Dense(num_class*128,# activation='relu', kernel_regularizer=regularizers.l1_l2( l1=l1_weight, l2=l2_weight) )) finetune_model.add(BatchNormalization()) finetune_model.add(Activation('relu')) #finetune_model.add(layers.Dense(num_class*64, activation='relu')) finetune_model.add(layers.Dense(num_class*32,# activation='relu', kernel_regularizer=regularizers.l1_l2( l1=l1_weight, l2=l2_weight) )) finetune_model.add(BatchNormalization()) finetune_model.add(Activation('relu')) #finetune_model.add(layers.Dense(num_class*16, activation='relu')) finetune_model.add(layers.Dense(num_class*8,# activation='relu', kernel_regularizer=regularizers.l1_l2( l1=l1_weight, l2=l2_weight) )) finetune_model.add(BatchNormalization()) finetune_model.add(Activation('relu')) finetune_model.add(layers.Dense(num_class, activation='softmax')) # Final Activation #finetune_model.summary() # Compile finetune_model.compile( loss = 'categorical_crossentropy', optimizer = 'adam', metrics=['acc'] ) history = finetune_model.fit( train_x, train_y, epochs=epoch, batch_size = batch_size, validation_data = (val_x, val_y) ) # Test Performance ''' TODO: Result 해결하는데 이슈가 있음 ### ! ''' y_pred = finetune_model.predict(test_x) #np.argmax y_pred = np.argmax(y_pred, axis=1) print('>> Predicted Results') print(y_pred) #test_y = np.argmax(test_y, axis=1) print('>> Ground Truth') print(test_y) accuracy = accuracy_score(test_y, y_pred) precision, recall, f1_score, _ = precision_recall_fscore_support(test_y, y_pred, average='micro') print(">> Test Performance <<") print('Acc: ', accuracy) print('Precision: ', precision) print('Recall: ', recall) print('F1 Score: ', f1_score)
validation_generator = validation_datagen.flow_from_directory( val_dir, target_size=params['image_size'], batch_size=params['batch_size'], class_mode='categorical') if ask_user('Data generators created. Continue to Network Initialisation?' ) == False: sys.exit(0) ######################################################################## # NETWORK INITIALISATION ######################################################################## net = InceptionV3(include_top=False, weights='imagenet', input_shape=params['receptive_field']) # Disable training in all but the last 4 layers for layer in net.layers: layer.trainable = False #for layer in net.layers[-17:]: # layer.trainable = True # And check it #for layer in net.layers: # print(layer, layer.trainable) model = Sequential()
import time from keras import backend as K K.tensorflow_backend._get_available_gpus() #os.environ["HTTPS_PROXY"] = "http://proxy.le.grp:8080" train_dir = './lego_fotos/train' validation_dir = './lego_fotos/validation' image_size = 224 from keras.applications import InceptionV3 #Load the VGG model vgg_conv = InceptionV3(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3)) # Freeze all the layers for layer in vgg_conv.layers[:-4]: layer.trainable = False # Check the trainable status of the individual layers for layer in vgg_conv.layers: print(layer, layer.trainable) from keras import models from keras import layers from keras import optimizers # Create the model
img_final = np.asarray(img_final) age_final = np.asarray(age_final) gdr_final = np.asarray(gdr_final) print("img_final shape:" + str(img_final.shape)) print("age_final shape:" + str(age_final.shape)) print("gdr_final shape:" + str(gdr_final.shape)) # First we need to create a model structure # input layer image_input = Input(shape=img_final.shape[1:], name="image_input") if CNN == 'IV3': # Inception V3 layer with pre-trained weights from ImageNet # base_iv3_model = InceptionV3(include_top=False, weights="imagenet") base_iv3_model = InceptionV3(weights="imagenet") # Inception V3 output from input layer output_vgg16 = base_iv3_model(image_input) # flattening it #why? # flat_iv3 = Flatten()(output_vgg16) elif CNN == 'RN50': # ResNet50 layer with pre-trained weights from ImageNet base_rn50_model = ResNet50(weights="imagenet") # ResNet50 output from input layer output_rn50 = base_rn50_model(image_input) elif CNN == 'Xception': # Xception layer with pre-trained weights from ImageNet base_xp_model = Xception(weights="imagenet") # Xception output from input layer output_xp = base_xp_model(image_input)
rm = ResultManager('results') agent_acc_size_dict = [] origin_acc_size_dict = [] agent = Q_Agent(s_dim=2, a_dim=10, epsilon_decay=0.9993, epsilon_min=0.2, lr=0.1, gamma=0.95) step_count = 0 env = BatchImgEnvironment(imagenet_train_path=images_dir, samples_per_class=3, backbone_model=InceptionV3(), backbone_model_input_size=(299, 299)) env.deep_model.compile(optimizer="Adam", loss="categorical_crossentropy", metrics=['top_k_categorical_accuracy']) env.reset() image_data_batchs = list_split(env.image_datalist, BATCH_SIZE) image_label_batchs = list_split(env.label_datalist, BATCH_SIZE) ref_size_batchs = list_split(env.ref_size_list, BATCH_SIZE) evaluation_result = defaultdict(list) for idx, image_batch in enumerate(image_data_batchs): performance = defaultdict(list)