import os import numpy as np from keras.preprocessing.image import img_to_array, load_img from data_utils import prepare_input_image_batch, postprocess_output, resize from model_utils import generate_mobilenet_model IMAGE_FOLDER_PATH = r"D:\Yue\Documents\Datasets\MSCOCO\val\valset\\" batch_size = 10 image_size = 256 model = generate_mobilenet_model(img_size=image_size) model.load_weights('weights/model.h5') X = [] files = os.listdir(IMAGE_FOLDER_PATH) files = files[:100] for i, filename in enumerate(files): img = img_to_array(load_img(os.path.join(IMAGE_FOLDER_PATH, filename))) / 255. img = resize(img, (image_size, image_size, 3)) * 255. # resize needs floats to be in 0-1 range, preprocess needs in 0-255 range X.append(img) if i % (len(files) // 20) == 0: print("Loaded %0.2f percentage of images from directory" % (i / float(len(files)) * 100)) X = np.array(X, dtype='float32') print("Images loaded. Shape = ", X.shape) X_lab, X_features = prepare_input_image_batch(X, batchsize=batch_size)
sepia_filter = np.array([[.393, .769, .189], [.349, .686, .168], [.272, .534, .131]]) scale = 0.01 sepia_img = img.dot(sepia_filter.T * scale) sepia_img /= sepia_img.max() sepia_img = skimage.img_as_ubyte(sepia_img) return sepia_img #variance_generator = lambda i,j,h: 0.25*(i+j)/1022. + 0.001 #variances = np.fromfunction(variance_generator,(224,224,3)) #sepia_img = skimage.util.random_noise(sepia_img, mode='gaussian', seed=1) #sepia_img = skimage.util.random_noise(sepia_img, mode='localvar', local_vars=variances) #return skimage.img_as_ubyte(skimage.color.rgb2gray(sepia_img)) return sepia_img model = generate_mobilenet_model() model.load_weights(_MODEL_WEIGHTS_NAME) def get_images_names(path): images = os.listdir(path) final = [] for image in images: final.append(image.rstrip()) return images images_names = get_images_names(_IMAGE_PATH) for img in images_names: x_b = img_to_array(load_img(_IMAGE_PATH+img, target_size= (224,224))) x_b = np.expand_dims(x_b, axis=0) x_b = x_b/255 #conversion to a range of -1 to 1. Explanation saved.
import os from data_utils import train_generator, val_batch_generator from model_utils import generate_mobilenet_model from train_utils import TensorBoardBatch from keras.callbacks import ModelCheckpoint nb_train_images = 60000 # there are 82783 images in MS-COCO, set this to how many samples you want to train on. batch_size = 125 model = generate_mobilenet_model(lr=1e-3) model.summary() # continue training if weights are available #if os.path.exists('weights/mobilenet_model.h5'): # model.load_weights('weights/mobilenet_model.h5') # use Batchwise TensorBoard callback tensorboard = TensorBoardBatch(batch_size=batch_size) checkpoint = ModelCheckpoint('weights/mobilenet_model_v2.h5', monitor='loss', verbose=1, save_best_only=True, save_weights_only=True) callbacks = [checkpoint, tensorboard] model.fit_generator(generator=train_generator(batch_size), steps_per_epoch=nb_train_images // batch_size, epochs=100, verbose=1,