def generate_samples_end_to_end(filename, patch_size, seed, batch_size=100): prng = random.Random() prng.seed(seed) batch = ([], []) with tarfile.open(filename, 'r:*') as tar: entries = [x.name for x in tar.getmembers() if x.isreg()] while True: random.shuffle(entries) for thing in entries: with tar.extractfile(thing) as jpeg: image = Image.open(jpeg) image.load() try: pix = np.array(image) dynamic_range_factor = 0.99 offset = (1 - dynamic_range_factor) / 2 if 3 == len( pix.shape ) and 3 == pix.shape[2] and pix.shape[0] > patch_size[ 0] and pix.shape[1] > patch_size[1]: for i in range(10): offset_row = math.floor( prng.uniform(0, pix.shape[0] - patch_size[0])) offset_col = math.floor( prng.uniform(0, pix.shape[1] - patch_size[1])) chip = dynamic_range_factor * pix[offset_row:( offset_row + patch_size[0]), offset_col:( offset_col + patch_size[1]), :].copy( ).astype(np.float32) / 255 + offset if (patch_size[0], patch_size[1], 3) == chip.shape: batch[0].append(chip) batch[1].append(chip) if len(batch[0]) == batch_size: yield np.stack(batch[0], axis=0), np.stack( batch[1], axis=0) batch[0].clear() batch[1].clear() gc.collect() del pix finally: image.close() del image
def load_single_image(source_image, img_size=(224, 224)): assert os.path.exists(source_image) single_image = np.zeros((1, img_size[0], img_size[1], 3)) image = Image.open(source_image) image = image.resize(img_size) image.load() image_background = Image.new("RGB", image.size, (255, 255, 255)) image_background.paste(image, mask=image.split()[3]) # Remove any alpha channel image = np.array(image_background) / 255. single_image[0] = image return single_image
def normalize(image): min_red = 255 min_green = 255 min_blue = 255 max_red = 0 max_green = 0 max_blue = 0 for i in range(IMAGE_WIDTH): for j in range(IMAGE_HEIGHT): red, green, blue = image.getpixel((i, j)) if red > max_red: max_red = red if red < min_red: min_red = red if green > max_green: max_green = green if green < min_green: min_green = green if blue > max_blue: max_blue = blue if blue < min_blue: min_blue = blue image_pix = image.load() for i in range(IMAGE_WIDTH): for j in range(IMAGE_HEIGHT): red, green, blue = image.getpixel((i, j)) modified_red = (red - min_red) / max_red * 255 modified_green = (green - min_green) / max_green * 255 modified_blue = (blue - min_blue) / max_blue * 255 image_pix[i, j] = (int(modified_red), int(modified_green), int(modified_blue), 255) return image
test_set = train_datagen.flow_from_directory( 'dataset/test_set', target_size = (64,64), batch_size = 32, class_mode = 'binary') from IPython.display import display from PIL import Image classifier.fit_generator( training_set, steps_per_epoch = 8000, epochs = 5, validation_data = test_set, validation_steps = 800) import numpy as np from keras.preprocessing import image test_image = image.load('sampleData.jpg', target_size = (64,64)) test_image = image.img_to_array(test_image) test_image = np.expand_dims(test_image, axis = 0) result = classifier.predict(test_image) if result[0][0] >= 0.5: prediction = 'Match' else: prediction = 'No Match' print(prediction)
import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' from keras.applications.vgg16 import VGG16 from keras.preprocessing.image import load_img as load from keras.preprocessing.image import img_to_array from keras.applications.vgg16 import preprocess_input from keras.applications.vgg16 import decode_predictions model = VGG16() image = load('../mug.jpg', target_size=(224, 224)) image = img_to_array(image) #the first entry is the numbers of images image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # subtract the mean RGB value, computed on the training set, from each pixel image = preprocess_input(image) #make prediction yhat = model.predict(image) label = decode_predictions(yhat) label = label[0][:3] print('%s (%.2f%%)' % (label[0][1], label[0][2] * 100)) print('%s (%.2f%%)' % (label[1][1], label[1][2] * 100)) print('%s (%.2f%%)' % (label[2][1], label[2][2] * 100))
shear_range=0.2, height_shift_range=0.07, zoom_range=0.2) gerador_teste = ImageDataGenerator(rescale=1. / 255) base_treinamento = gerador_treinamento.flow_from_directory( 'dataset/training_set', target_size=(64, 64), batch_size=32, class_mode='binary') base_teste = gerador_teste.flow_from_directory('dataset/test_set', target_size=(64, 64), batch_size=32, class_mode='binary') classificador.fit_generator(base_treinamento, steps_per_epoch=4000 / 32, epochs=5, validation_data=base_teste, validation_steps=1000 / 32) imagem_teste = image.load('/dataset/test_set/gato/cat.3500.jpg', target_size=(64, 64)) imagem_teste = image.img_to_array(imagem_teste) imagem_teste /= 255 imagem_teste = np.expand_dims(imagem_teste, axis=0) previsao = classificador.predict(imagem_teste) base_treinamento.class_indices
class_mode='binary') ## start time of training from datetime import datetime now = datetime.now() current_time = now.strftime("%H:%M:%S") print("Current Time =", current_time) classifier.fit_generator(training_set, steps_per_epoch=8000, epochs=25, validation_data=test_set, validation_steps=2000) classifier.save('h5') ## training end print("ends") now = datetime.now() current_time = now.strftime("%H:%M:%S") print("Current Time =", current_time) import numpy as np from keras.preprocessing import image test_image = image.load('dataset/single_prediction/cat_or_dog_1.jpg', target_size=(64, 64)) test_image = image.img_to_array(test_image) test_image = np.expand_dims(test_image, axis=0) resu