def testRotation(self): from generator import ImageGenerator batch1 = ImageGenerator(self.file_path, self.label_path, 100, [32, 32, 3], rotation=False, mirroring=False, shuffle=False).next()[0] batch2 = ImageGenerator(self.file_path, self.label_path, 100, [32, 32, 3], rotation=True, mirroring=False, shuffle=False).next()[0] # determine the images which were augmented augmented_images_indices = np.sum(np.abs(batch1 - batch2), axis=(1, 2, 3)).astype(np.bool_) # extract corner points for each sample and reduce augmented to one characateristic row # this row is also inverted for simpler computations augmented_corners = self._get_corner_points(batch2[augmented_images_indices]) characteristic_corners = augmented_corners[:, 0, ::-1] original_corners = self._get_corner_points(batch1[augmented_images_indices]) # subtract characteristic corners to original corners. after summing spacial and channel dimensions, 0 are expected # e.g. if sample 2 is rotated by 90, we have a 0 in rot1 at the 2nd posistion see similiar to testMirroring rot1 = np.sum(original_corners[:, :, 0] - characteristic_corners, axis=(1, 2)) rot2 = np.sum(original_corners[:, 1, :] - characteristic_corners, axis=(1, 2)) rot3 = np.sum(original_corners[:, ::-1, 1] - characteristic_corners, axis=(1, 2)) # assumption is that augmented images are either rotated by 90 (rot1), 180 (ro2) or 270 (rot3) degrees, thus # their elementwise product must be zero np.testing.assert_raises(AssertionError, np.testing.assert_array_equal, batch1, batch2) np.testing.assert_almost_equal(np.sum(rot1 * rot2 * rot3), 0)
def testShuffle(self): # Creates two image generator objects. # Since shuffle is enabled for one image generator the output should be different. from generator import ImageGenerator gen = ImageGenerator(self.file_path, self.label_path, 10, [32, 32, 3], rotation=False, mirroring=False, shuffle=True) gen2 = ImageGenerator(self.file_path, self.label_path, 10, [32, 32, 3], rotation=False, mirroring=False, shuffle=False) np.testing.assert_raises(AssertionError, np.testing.assert_array_equal, gen.next()[0], gen2.next()[0]) np.testing.assert_raises(AssertionError, np.testing.assert_array_equal, gen.next()[1], gen2.next()[1])
def testResetIndex(self): # Data contains 100 image samples, for a batchsize of 60 an # overlap of 20 occurs, therefore the first 20 elements # of the first batch should be equal to the last 20 of the second batch from generator import ImageGenerator gen = ImageGenerator(self.file_path, self.label_path, 60, [32, 32, 3], rotation=False, mirroring=False, shuffle=False) b1 = gen.next()[0] b2 = gen.next()[0] np.testing.assert_almost_equal(b1[:20], b2[40:])
def testResetIndex(self): from generator import ImageGenerator # Data contains 100 image samples, create two overlapping batches gen = ImageGenerator(self.file_path, self.json_path, 60, [32, 32, 3], rotation=False, mirroring=False, shuffle=False) b1 = gen.next()[0] b2 = gen.next()[0] np.testing.assert_almost_equal(b1[:20], b2[40:])
def testLabels(self): # this test makes sure your generator returns integers as labels and not any strings or floating point values # if this one fails make sure you cast your array values to integers from generator import ImageGenerator label = ImageGenerator(self.file_path, self.label_path, 12, [50, 50, 3], rotation=False, mirroring=False, shuffle=False).next()[1] self.assertFalse(isinstance(label[0], str)) self.assertTrue(np.issubdtype(np.array(label).dtype, np.integer))
def testInit(self): # Creates two image generator objects without shuffling. # Calling next on either one should result in the same output from generator import ImageGenerator gen = ImageGenerator(self.file_path, self.label_path, 12, [32, 32, 3], rotation=False, mirroring=False, shuffle=False) gen2 = ImageGenerator(self.file_path, self.label_path, 12, [32, 32, 3], rotation=False, mirroring=False, shuffle=False) np.testing.assert_almost_equal(gen.next[0], gen2.next[0]) np.testing.assert_almost_equal(gen.next[1], gen2.next[1])
def testResize(self): from generator import ImageGenerator batch = ImageGenerator(self.file_path, self.json_path, 12, [50, 50, 3], rotation=False, mirroring=False, shuffle=False).next()[0] self.assertEqual(batch.shape, (12, 50, 50, 3))
def testMirroring(self): from generator import ImageGenerator batch1 = ImageGenerator(self.file_path, self.label_path, 12, [32, 32, 3], rotation=False, mirroring=False, shuffle=False).next()[0] batch2 = ImageGenerator(self.file_path, self.label_path, 12, [32, 32, 3], rotation=False, mirroring=True, shuffle=False).next()[0] # determine the images which were augmented augmented_images_indices = np.sum(np.abs(batch1 - batch2), axis=(1, 2, 3)).astype(np.bool_) # extract corner points for each sample augmented_corners = self._get_corner_points(batch2[augmented_images_indices]) original_corners = self._get_corner_points(batch1[augmented_images_indices]) # pick vertical ref points and compare them with opposites in original corners # compute then the diff and sum over vertical axis and channels. those images which were flipped vertically # contain now zeros vertical_augmented_corners = augmented_corners[:, :, 0] vertical = np.sum(original_corners[:, :, 1, :] - vertical_augmented_corners, axis=(1, 2)) # pick horizontal ref points and compare them with opposites in original corners # compute then the diff and sum over horizontal axis and channels. those images which were flipped horizontally # contain now zeros horizontal_augmented_corners = augmented_corners[:, 0, :] horizontal = np.sum(original_corners[:, 1, :, :] - horizontal_augmented_corners, axis=(1, 2)) # pick top left corner and bottom right corner (diagonals are flipped if double mirror) vertical_horizontal_augmented_corners = np.stack( list(zip(augmented_corners[:, 0, 0, :], augmented_corners[:, 1, 1, :]))) original_corner_diagonals = np.stack( list(zip(original_corners[:, 1, 1, :], original_corners[:, 0, 0, :]))) horizontal_vertical = np.sum(original_corner_diagonals - vertical_horizontal_augmented_corners, axis=(1, 2)) # the elementwise product of horizontal and vertical must be zero # since the images can only be augmented with vertical or horizontal mirroring np.testing.assert_raises(AssertionError, np.testing.assert_array_equal, batch1, batch2) np.testing.assert_almost_equal(np.sum(vertical * horizontal * horizontal_vertical), 0)
def fit(self): self.model = Sequential() self.model.add( Conv2D(32, (2, 2), padding='valid', strides=1, input_shape=(1024, 1024, 1))) self.model.add(Activation('relu')) self.model.add(Conv2D(32, (2, 2))) self.model.add(Activation('relu')) self.model.add(Conv2D(32, (2, 2))) self.model.add(Activation('relu')) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Conv2D(64, (4, 4))) self.model.add(Activation('relu')) self.model.add(Conv2D(64, (4, 4))) self.model.add(Activation('relu')) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Flatten()) self.model.add(Dense(4096)) self.model.add(Activation('relu')) self.model.add(Dropout(0.2)) self.model.add(Dense(4096)) self.model.add(Activation('relu')) self.model.add(Dropout(0.2)) self.model.add(Dense(self.classes)) self.model.add(Activation('softmax')) self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) self.model.fit_generator(generator=ImageGenerator( X_train, y_train, self.batch_size), batch_size=self.batch_size, epochs=self.epoch, verbose=1, validation_split=0.2, class_weight='auto', callbacks=[ EarlyStopping(monitor='acc', min_delta=0.001, patience=2, verbose=0, mode='auto') ])
def testShuffle(self): from generator import ImageGenerator gen = ImageGenerator(self.file_path, self.json_path, 10, [32, 32, 3], rotation=False, mirroring=False, shuffle=True) gen2 = ImageGenerator(self.file_path, self.json_path, 10, [32, 32, 3], rotation=False, mirroring=False, shuffle=False) np.testing.assert_raises(AssertionError, np.testing.assert_array_equal, gen.next()[0], gen2.next()[0]) np.testing.assert_raises(AssertionError, np.testing.assert_array_equal, gen.next()[1], gen2.next()[1])
def testInit(self): from generator import ImageGenerator gen = ImageGenerator(self.file_path, self.json_path, 12, [32, 32, 3], rotation=False, mirroring=False, shuffle=False) gen2 = ImageGenerator(self.file_path, self.json_path, 12, [32, 32, 3], rotation=False, mirroring=False, shuffle=False) np.testing.assert_almost_equal(gen.next()[0], gen2.next()[0]) np.testing.assert_almost_equal(gen.next()[1], gen2.next()[1])
#C = pat.Checkers(100, 25) #C.draw() #C.show() #RGB = pat.Spectrum(255) #RGB.draw() #RGB.show() #Circ = pat.Circle(1024, 200, (512, 256)) #Circ.draw() #Circ.show() from generator import ImageGenerator label_path = '/home/shrihari/Downloads/Deep Learning/Exercise1/src_to_implement/data/Labels.json' file_path = '/home/shrihari/Downloads/Deep Learning/Exercise1/src_to_implement/data/exercise_data/' gen = ImageGenerator(file_path, label_path, 12, [32, 32, 3], rotation=True, mirroring=False, shuffle=True) #b,l = gen.next() #print(b) #print(l) gen.show()
print('Test Image Count: {:d}'.format(len(test_images))) model_option = 'fcn' #Resize images to be fed into model if model_option=='random': resize_images = (350,525) #Random baseline resize elif model_option=='fcn': resize_images = (448,672) #FCN resize elif model_option=='mask-rcnn': resize_images = (1400,2100) #Resize is handled by config else: print('Please choose a valid model option') batch_size = math.ceil(len(test_images) / 100) #Use for random baseline and FCN test_generator = ImageGenerator(test_images, batch_size=batch_size, resize=resize_images) #Open file for writing f = open('submission.csv', 'w+') f.write('Image_Label,EncodedPixels\n') #Write pixel encoded image masks out to file in batches stop_flag = False iteration = 1 for b, batch in enumerate(test_generator): while not stop_flag: if iteration*batch_size > len(test_images): batch = batch[0:(len(test_images)-iteration*batch_size)] stop_flag = True #Write predictions to submission file print('Running Batch {:d}'.format(iteration))
from pattern import Checker from pattern import Circle from generator import ImageGenerator # Checkerboard a = Checker(250, 25) a.draw() a.show() # Circle b = Circle(1024, 200, (512, 256)) b.draw() b.show() # Image generator ima_gen = ImageGenerator('./exercise_data/', './Labels.json', 12, [32, 32, 3], rotation=False, mirroring=False, shuffle=False) ima_gen.show(resize=True)
from flask import Flask, escape, request, jsonify, abort, request, send_from_directory from flask_restplus import Api, Resource, reqparse, fields from generator import ImageGenerator import json import uuid import os from cleanup import clean_images IMAGE_DIR = "{}/api/static/cleaned-images".format(os.getcwd()) # flask app configuration app = Flask(__name__, static_folder='static') api = Api(app, doc="/swagger/") # image gnerator generator = ImageGenerator() UPLOAD_DIRECTORY = "./api/static/images" fields = api.model( 'NewPokemonImageRequestBody', { 'id1': fields.Integer(description='pokedex id', required=True, min=1), 'id2': fields.Integer(description='pokedex id', required=True, min=1), }) @api.route("/home") class Home(Resource): @api.doc("home") def get(self): return jsonify("Welcome to PokeMate image generator!")