def predict(p): img_file = "../../imagenetv2-matched-frequency/{}/{}.jpeg".format( p.cls, p.im) # print("Predict {}".format(img_file)) image = imread(img_file) x = center_crop_and_resize(image, image_size=p.image_size) x = preprocess_input(x) x = np.expand_dims(x, 0) # make prediction and decode y = model.predict(x) pred_class = np.argmax(y) # print("Predicted class: {}".format(pred_class)) # v = decode_predictions(y) # Evaluate accuracy p.total_imgs += 1.0 p.top_1_correct += (pred_class == p.cls) # Loop through folders and images p.im += 1 if p.im == p.NUM_IMAGES: p.cls = p.cls + 1 if p.cls < p.NUM_CLASSES - 1 else 0 p.im = 0 print("Entering class: {}".format(p.cls))
def extract_feature(model, image_path): try: img = pil_loader(image_path) img = img.resize((256, 256)) img_data = image.img_to_array(img) except: img_data = np.zeros((256, 256, 3)) img_data = np.expand_dims(img_data, axis=0) img_data = preprocess_input(img_data) feature = model.predict(img_data) return feature
def generate_dataset_main(n=10000, save_path=None, seed=None, model_res=1024, image_size=256, minibatch_size=32, truncation=0.7): """ Generates a dataset of 'n' images of shape ('size', 'size', 3) with random seed 'seed' along with their dlatent vectors W of shape ('n', 512) These datasets can serve to train an inverse mapping from X to W as well as explore the latent space More variation added to latents; also, negative truncation added to balance these examples. """ n = n // 2 # this gets doubled because of negative truncation below model_scale = int(2 * (math.log(model_res, 2) - 1)) # For example, 1024 -> 18 Gs = load_Gs() if (model_scale % 3 == 0): mod_l = 3 else: mod_l = 2 if seed is not None: b = bool(np.random.RandomState(seed).randint(2)) Z = np.random.RandomState(seed).randn(n * mod_l, Gs.input_shape[1]) else: b = bool(np.random.randint(2)) Z = np.random.randn(n * mod_l, Gs.input_shape[1]) if b: mod_l = model_scale // 2 mod_r = model_scale // mod_l if seed is not None: Z = np.random.RandomState(seed).randn(n * mod_l, Gs.input_shape[1]) else: Z = np.random.randn(n * mod_l, Gs.input_shape[1]) W = Gs.components.mapping.run( Z, None, minibatch_size=minibatch_size ) # Use mapping network to get unique dlatents for more variation. dlatent_avg = Gs.get_var('dlatent_avg') # [component] W = (W[np.newaxis] - dlatent_avg) * np.reshape([truncation, -truncation], [ -1, 1, 1, 1 ]) + dlatent_avg # truncation trick and add negative image pair W = np.append(W[0], W[1], axis=0) W = W[:, :mod_r] W = W.reshape((n * 2, model_scale, 512)) X = Gs.components.synthesis.run(W, randomize_noise=False, minibatch_size=minibatch_size, print_progress=True, output_transform=dict( func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)) X = np.array([ cv2.resize(x, (image_size, image_size), interpolation=cv2.INTER_AREA) for x in X ]) X = preprocess_input(X) return W, X
def batch_preprocess_input(x_batch, network): if network == 'Xception': x_batch = xception.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'VGG16': x_batch = vgg16.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'VGG19': x_batch = vgg19.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'ResNet50' or network == 'ResNet101' or network == 'ResNet152': x_batch = resnet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'ResNet50V2' or network == 'ResNet101V2' or network == 'ResNet152V2': x_batch = resnet_v2.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'ResNeXt50' or network == 'ResNeXt101': x_batch = resnext.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'InceptionV3': x_batch = inception_v3.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'InceptionResNetV2': x_batch = inception_resnet_v2.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'MobileNet': x_batch = mobilenet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'MobileNetV2': x_batch = mobilenet_v2.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'DenseNet121' or network == 'DenseNet169' or network == 'DenseNet201': x_batch = densenet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'NASNetMobile' or network == 'NASNetLarge': x_batch = nasnet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif 'EfficientNet' in network: x_batch = efficientnet.preprocess_input(x_batch) else: return None return x_batch
def randaug(imgs): # for i in range(imgs.shape[0]): # imgs = distort_image_with_randaugment(imgs.astype('uint8'), 2, 18) imgs = preprocess_input(imgs) return imgs
def _get_panda_input(input_shape): image = imread(PANDA_PATH) image = efn.center_crop_and_resize(image, input_shape[1]) image = efn.preprocess_input(image) image = np.expand_dims(image, 0) return image