def preprocess_input(x): """Preprocesses a numpy array encoding a batch of images. # Arguments x: a 4D numpy array consists of RGB values within [0, 255]. # Returns Input array scaled to [-1.,1.] """ return preprocess_input(x, mode='tf')
def preprocess_input(x, **kwargs): """Preprocesses a numpy array encoding a batch of images. # Arguments x: a 4D numpy array consists of RGB values within [0, 255]. # Returns Preprocessed array. """ return imagenet_utils.preprocess_input(x, mode="tf", **kwargs)
def test_preprocess_input_symbolic(self): # Test image batch x = np.random.uniform(0, 255, (2, 10, 10, 3)) inputs = keras.layers.Input(shape=x.shape[1:]) outputs = keras.layers.Lambda( preprocess_input, output_shape=x.shape[1:])(inputs) model = keras.models.Model(inputs, outputs) assert model.predict(x).shape == x.shape # pylint: disable=g-long-lambda outputs1 = keras.layers.Lambda(lambda x: preprocess_input(x, 'channels_last'), output_shape=x.shape[1:])(inputs) model1 = keras.models.Model(inputs, outputs1) out1 = model1.predict(x) x2 = np.transpose(x, (0, 3, 1, 2)) inputs2 = keras.layers.Input(shape=x2.shape[1:]) # pylint: disable=g-long-lambda outputs2 = keras.layers.Lambda(lambda x: preprocess_input(x, 'channels_first'), output_shape=x2.shape[1:])(inputs2) model2 = keras.models.Model(inputs2, outputs2) out2 = model2.predict(x2) self.assertAllClose(out1, out2.transpose(0, 2, 3, 1)) # Test single image x = np.random.uniform(0, 255, (10, 10, 3)) inputs = keras.layers.Input(shape=x.shape) outputs = keras.layers.Lambda(preprocess_input, output_shape=x.shape)(inputs) model = keras.models.Model(inputs, outputs) assert model.predict(x[np.newaxis])[0].shape == x.shape # pylint: disable=g-long-lambda outputs1 = keras.layers.Lambda(lambda x: preprocess_input(x, 'channels_last'), output_shape=x.shape)(inputs) model1 = keras.models.Model(inputs, outputs1) out1 = model1.predict(x[np.newaxis])[0] x2 = np.transpose(x, (2, 0, 1)) inputs2 = keras.layers.Input(shape=x2.shape) outputs2 = keras.layers.Lambda(lambda x: preprocess_input(x, 'channels_first'), output_shape=x2.shape)(inputs2) # pylint: disable=g-long-lambda model2 = keras.models.Model(inputs2, outputs2) out2 = model2.predict(x2[np.newaxis])[0] self.assertAllClose(out1, out2.transpose(1, 2, 0))
def test_preprocess_input_symbolic(self): # Test image batch x = np.random.uniform(0, 255, (2, 10, 10, 3)) inputs = keras.layers.Input(shape=x.shape[1:]) outputs = keras.layers.Lambda(preprocess_input, output_shape=x.shape[1:])(inputs) model = keras.models.Model(inputs, outputs) assert model.predict(x).shape == x.shape # pylint: disable=g-long-lambda outputs1 = keras.layers.Lambda( lambda x: preprocess_input(x, 'channels_last'), output_shape=x.shape[1:])(inputs) model1 = keras.models.Model(inputs, outputs1) out1 = model1.predict(x) x2 = np.transpose(x, (0, 3, 1, 2)) inputs2 = keras.layers.Input(shape=x2.shape[1:]) # pylint: disable=g-long-lambda outputs2 = keras.layers.Lambda( lambda x: preprocess_input(x, 'channels_first'), output_shape=x2.shape[1:])(inputs2) model2 = keras.models.Model(inputs2, outputs2) out2 = model2.predict(x2) self.assertAllClose(out1, out2.transpose(0, 2, 3, 1)) # Test single image x = np.random.uniform(0, 255, (10, 10, 3)) inputs = keras.layers.Input(shape=x.shape) outputs = keras.layers.Lambda(preprocess_input, output_shape=x.shape)(inputs) model = keras.models.Model(inputs, outputs) assert model.predict(x[np.newaxis])[0].shape == x.shape # pylint: disable=g-long-lambda outputs1 = keras.layers.Lambda( lambda x: preprocess_input(x, 'channels_last'), output_shape=x.shape)(inputs) model1 = keras.models.Model(inputs, outputs1) out1 = model1.predict(x[np.newaxis])[0] x2 = np.transpose(x, (2, 0, 1)) inputs2 = keras.layers.Input(shape=x2.shape) outputs2 = keras.layers.Lambda( lambda x: preprocess_input(x, 'channels_first'), output_shape=x2.shape)(inputs2) # pylint: disable=g-long-lambda model2 = keras.models.Model(inputs2, outputs2) out2 = model2.predict(x2[np.newaxis])[0] self.assertAllClose(out1, out2.transpose(1, 2, 0))
def normalize(self, tensor: np.ndarray) -> np.ndarray: # This is equivalent to: # x /= 255. # mean = [0.485, 0.456, 0.406] # std = [0.229, 0.224, 0.225] # when mode is torch return imagenet_utils.preprocess_input(tensor, data_format=None, mode="torch")
def loadImage(imageInsect): if imageInsect.mode != 'RGB': imageInsect = imageInsect.convert('RGB') image = imageInsect.resize((200, 200)) image = img_to_array(image) image = np.expand_dims(image, 0) image = imagenet_utils.preprocess_input(image) image = image / 255 return image
def load_image_from_file_or_url(image_filename_or_url: str) -> np.ndarray: logger.info(f'Received image url or file {image_filename_or_url}.') if is_url(image_filename_or_url): logger.info('Loading from uri.') image = load_image_from_url(image_filename_or_url) else: logger.info('Loading from image file.') image = load_image_from_file(image_filename_or_url) image = preprocess_input(image_utils.img_to_array(image), mode='tf') return np.expand_dims(image, axis=0)
def preprocess_input(x): """Preprocesses a numpy array encoding a batch of images. Arguments: x: a 4D numpy array consists of RGB values within [0, 255]. Returns: Preprocessed array. """ return imagenet_utils.preprocess_input(x, mode='tf')
def preprocess_input(x, data_format=None): """Preprocesses a numpy array encoding a batch of images. Arguments: x: a 3D or 4D numpy array consists of RGB values within [0, 255]. data_format: data format of the image tensor. Returns: Preprocessed array. """ return imagenet_utils.preprocess_input(x, data_format, mode='torch')
def preprocess_input(x, data_format=None): """Preprocesses a numpy array encoding a batch of images. Arguments x: A 4D numpy array consists of RGB values within [0, 255]. Returns Preprocessed array. Raises ValueError: In case of unknown `data_format` argument. """ return imagenet_utils.preprocess_input(x, data_format=data_format, mode='caffe')
def run(self, filepath, conf_thresh=0.4): """ """ frame = cv.imread(filepath) src = np.copy(frame) resized = cv.resize(frame, (self.width, self.height)) rgb = cv.cvtColor(resized, cv.COLOR_BGR2RGB) src_shape = src.shape inputs = [img_to_array(rgb)] x = preprocess_input(np.array(inputs)) y = self.model.predict(x) results = self.bbox_util.detection_out(y) to_draw = cv.resize(resized, (int(src_shape[1]), int(src_shape[0]))) if len(results) > 0 and len(results[0]) > 0: # Interpret output, only one frame is used det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin = results[0][:, 2] det_ymin = results[0][:, 3] det_xmax = results[0][:, 4] det_ymax = results[0][:, 5] top_indices = [ i for i, conf in enumerate(det_conf) if conf >= conf_thresh ] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin = det_xmin[top_indices] top_ymin = det_ymin[top_indices] top_xmax = det_xmax[top_indices] top_ymax = det_ymax[top_indices] for i in range(top_conf.shape[0]): xmin = int(round(top_xmin[i] * to_draw.shape[1])) ymin = int(round(top_ymin[i] * to_draw.shape[0])) xmax = int(round(top_xmax[i] * to_draw.shape[1])) ymax = int(round(top_ymax[i] * to_draw.shape[0])) class_num = int(top_label_indices[i]) cv.rectangle(src, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2) text = self.name_classes[class_num] + " " + ('%.2f' % top_conf[i]) text_top = (xmin, ymin - 10) text_bot = (xmin + 80, ymin + 5) text_pos = (xmin + 5, ymin) cv.rectangle(src, text_top, text_bot, (0, 255, 0), -1) cv.putText(src, text, text_pos, cv.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 0), 1) cv.imshow("pic", src) cv.waitKey(0) cv.destroyAllWindows() cv.imwrite("output.png", src)
def preprocess_input(x, data_format=None): """Preprocesses a numpy array encoding a batch of images. Arguments x: A 4D numpy array consists of RGB values within [0, 255]. Returns Preprocessed array. """ return imagenet_utils.preprocess_input(x, data_format=data_format, mode='tf')
def model_predict(img, model): img = img.resize(224, 224) # Preprocessing the image x = image.img_to_array(img) # x = np.true_divide(x, 255) x = np.expand_dims(x, axis=0) # Be careful how your trained model deals with the input # otherwise, it won't make correct prediction! x = preprocess_input(x, mode=model) preds = model.predict(x) return preds
def feed_batch_image(files): new_images = [] for f in files: filename = f original_image = load_img(filename, target_size=(imgs_width, imgs_height)) numpy_image = img_to_array(original_image) image_batch = np.expand_dims(numpy_image, axis=0) new_images.append(image_batch) images = np.vstack(new_images) processed_imgs = preprocess_input(images.copy()) return processed_imgs
def test_preprocess_input(self): # Test batch of images x = np.random.uniform(0, 255, (2, 10, 10, 3)) self.assertEqual(preprocess_input(x).shape, x.shape) out1 = preprocess_input(x, 'channels_last') out2 = preprocess_input(np.transpose(x, (0, 3, 1, 2)), 'channels_first') self.assertAllClose(out1, out2.transpose(0, 2, 3, 1)) # Test single image x = np.random.uniform(0, 255, (10, 10, 3)) self.assertEqual(preprocess_input(x).shape, x.shape) out1 = preprocess_input(x, 'channels_last') out2 = preprocess_input(np.transpose(x, (2, 0, 1)), 'channels_first') self.assertAllClose(out1, out2.transpose(1, 2, 0))
def init_resnet(stack_fn, preact, use_bias, model_name, input_shape, block_name_to_hyperparameters_dict, preprocess_input_mode): # Downloads the weights file_name = model_name + "_weights_tf_dim_ordering_tf_kernels_notop.h5" file_hash = WEIGHTS_HASHES[model_name][1] weights_path = data_utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir="models", file_hash=file_hash) # Define and initialize the first block first_block = ResNet(stack_fn=lambda x: x, preact=preact, use_bias=use_bias, include_top=False, weights=None, input_shape=input_shape) first_block.load_weights(weights_path, by_name=True) submodel_list = [first_block] # Define and initialize each block for block_name, (filters, blocks, stride1) in block_name_to_hyperparameters_dict.items(): input_tensor = Input(shape=K.int_shape(submodel_list[-1].output)[1:]) output_tensor = stack_fn(input_tensor, filters=filters, blocks=blocks, stride1=stride1, name=block_name) submodel = Model(inputs=input_tensor, outputs=output_tensor, name="{}_block".format(block_name)) submodel.load_weights(weights_path, by_name=True) submodel_list.append(submodel) return submodel_list, lambda x: preprocess_input(x, mode=preprocess_input_mode)
def test_preprocess_input(self): # Test invalid mode check x = np.random.uniform(0, 255, (10, 10, 3)) with self.assertRaises(ValueError): utils.preprocess_input(x, mode='some_unknown_mode') # Test image batch with float and int image input x = np.random.uniform(0, 255, (2, 10, 10, 3)) xint = x.astype('int32') self.assertEqual(utils.preprocess_input(x).shape, x.shape) self.assertEqual(utils.preprocess_input(xint).shape, xint.shape) out1 = utils.preprocess_input(x, 'channels_last') out1int = utils.preprocess_input(xint, 'channels_last') out2 = utils.preprocess_input(np.transpose(x, (0, 3, 1, 2)), 'channels_first') out2int = utils.preprocess_input(np.transpose(xint, (0, 3, 1, 2)), 'channels_first') self.assertAllClose(out1, out2.transpose(0, 2, 3, 1)) self.assertAllClose(out1int, out2int.transpose(0, 2, 3, 1)) # Test single image x = np.random.uniform(0, 255, (10, 10, 3)) xint = x.astype('int32') self.assertEqual(utils.preprocess_input(x).shape, x.shape) self.assertEqual(utils.preprocess_input(xint).shape, xint.shape) out1 = utils.preprocess_input(x, 'channels_last') out1int = utils.preprocess_input(xint, 'channels_last') out2 = utils.preprocess_input(np.transpose(x, (2, 0, 1)), 'channels_first') out2int = utils.preprocess_input(np.transpose(xint, (2, 0, 1)), 'channels_first') self.assertAllClose(out1, out2.transpose(1, 2, 0)) self.assertAllClose(out1int, out2int.transpose(1, 2, 0)) # Test that writing over the input data works predictably for mode in ['torch', 'tf']: x = np.random.uniform(0, 255, (2, 10, 10, 3)) xint = x.astype('int') x2 = utils.preprocess_input(x, mode=mode) xint2 = utils.preprocess_input(xint) self.assertAllClose(x, x2) self.assertNotEqual(xint.astype('float').max(), xint2.max()) # Caffe mode works differently from the others x = np.random.uniform(0, 255, (2, 10, 10, 3)) xint = x.astype('int') x2 = utils.preprocess_input(x, data_format='channels_last', mode='caffe') xint2 = utils.preprocess_input(xint) self.assertAllClose(x, x2[..., ::-1]) self.assertNotEqual(xint.astype('float').max(), xint2.max())
def preprocess_input(x): return preprocess_input(x, mode='tf')
def __getitem__(self, idx, preprocess=False): """ TODO: refactor now that we aren't adding individual options as we test things out during the challenge. `pre_image` and `post_image` are the pre-disaster and post-disaster samples. `premask` and `postmask` are the uint8, grayscale (single channel) masks for each. """ if self.shuffle is True and self._do_shuffle is True: self._do_shuffle = False random.shuffle(self.samples) elif self.shuffle is True and idx == len(self)-1: # reshuffle samples next call self._do_shuffle = True x_avgs = [] x_pres = [] y_pres = [] x_posts = [] y_posts = [] stacked = [] #x_pre = np.empty(0) #x_post = np.empty(0) #y_pre = np.empty(0) #y_post = np.empty(0) return_postmask = self.return_postmask for (pre, post) in self.samples[idx*self.batch_size:(idx+1)*self.batch_size]: if 'test' in pre.img_name or 'test' in post.img_name: x_pre, x_post = pre.image(), post.image() if self.return_average is True: return np.expand_dims( (x_pre.astype(np.uint32) + x_post.astype(np.uint32) / 2).astype(np.uint8), axis=0 ), pre.img_path elif self.return_stacked is True: return np.expand_dims(np.dstack([x_pre, x_post]), axis=0), pre.img_path else: return (x_pre, x_post), pre.img_path premask = pre.sm_multichannelmask() if self.segmentation_models else pre.multichannelmask() postmask = post.sm_multichannelmask() if self.segmentation_models else post.multichannelmask() if not return_postmask: premask = convert_postmask_to_premask(postmask.copy()) preimg = pre.image() postimg = post.image() avgimg = ((preimg.astype(np.uint32) + postimg.astype(np.uint32)) / 2).astype(np.uint8) # un-classified screws training up (test set doesn't include that value), so we zero them all out preimg, postimg, postmask = eliminate_unclassified(preimg, postimg, postmask) if preprocess is True: if not self.return_average: preimg = preprocess_input(preimg) postimg = preprocess_input(postimg) else: avgimg = preprocess_input(avgimg) # training deformations if isinstance(self.transform, float) and random.random() < float(self.transform): # Rotate 90-270 degrees rotate_dict = { 'theta': 90 * random.randint(1, 3),} # rotate the sample and mask by the same angle together if not self.return_average: preimg = self.image_datagen.apply_transform(preimg, rotate_dict) postimg = self.image_datagen.apply_transform(postimg, rotate_dict) else: avgimg = self.image_datagen.apply_transform(avgimg, rotate_dict) premask = self.image_datagen.apply_transform(premask, rotate_dict) postmask = self.image_datagen.apply_transform(postmask, rotate_dict) if isinstance(self.transform, float) and random.random() < float(self.transform): # apply a Gaussian blur to the sample, but not the mask ksize = 3 if not self.return_average: preimg = apply_gaussian_blur(preimg, kernel=(ksize,ksize)) postimg = apply_gaussian_blur(postimg, kernel=(ksize,ksize)) else: avgimg = apply_gaussian_blur(avgimg, kernel=(ksize,ksize)) if not self.return_average: x_pre = np.array(preimg, copy=False).astype(np.int32) x_post = np.array(postimg, copy=False).astype(np.int32) else: x_avg = np.array(avgimg, copy=False) if self.return_single_channel is True: # segmentation_models compatibility y_pre = np.array(premask.astype(int)) y_post = np.array(postmask.astype(int)) #x_pre = tf.compat.v1.image.resize(x_pre, (1008, 1008), align_corners=True) #x_post = tf.compat.v1.image.resize(x_post, (1008, 1008), align_corners=True) else: y_pre = np.array(premask.astype(int).reshape(S.MASKSHAPE[0]*S.MASKSHAPE[1], -1), copy=False) y_post = np.array(postmask.astype(int).reshape(S.MASKSHAPE[0]*S.MASKSHAPE[1], -1), copy=False) #y_pre = np.array(premask.astype(int), copy=False) #y_post = np.array(postmask.astype(int), copy=False) #y_pre = convert_prediction(y_pre) #y_post = convert_prediction(y_post) #y_pre = y_pre.reshape(1, 1024, 1024, 1) #y_post = y_pre.reshape(1, 1024, 1024, 1) if interlace is True: x_pre, x_post = interlace(x_pre, x_post) elif self.return_stacked is True: stacked.append(np.dstack([x_pre, x_post])) if not self.return_average: x_pres.append(x_pre) x_posts.append(x_post) else: x_avgs.append(x_avg) y_pres.append(y_pre) y_posts.append(y_post) #x_pre = np.array(pre.chips(preimg)).astype(np.int32) #x_post = np.array(post.chips(postimg)).astype(np.int32) #y_pre = np.array([chip.astype(int).reshape(S.MASKSHAPE[0]*S.MASKSHAPE[1], S.N_CLASSES) for chip in pre.chips(premask)]) #y_post = np.array([chip.astype(int).reshape(S.MASKSHAPE[0]*S.MASKSHAPE[1], 6) for chip in post.chips(postmask)]) if not self.return_average: x_pres = np.array(x_pres, copy=False) x_posts = np.array(x_posts, copy=False) else: x_avgs = np.array(x_avgs, copy=False) y_pres = np.array(y_pres, copy=False) y_posts = np.array(y_posts, copy=False) y_ret = y_posts if return_postmask is True else y_pres if self.return_average is True: return np.array(x_avgs, copy=False), y_ret elif self.return_post_only is True: return x_posts, y_ret elif self.return_stacked is True: return np.array(stacked, copy=False), y_ret else: return (x_pres, x_posts), y_ret
def preprocess_input(x, data_format=None): """Preprocesses the input (encoding a batch of images) to the VGG16 model.""" return imagenet_utils.preprocess_input(x, data_format=data_format, mode='caffe')
def create(n_classes=1, base=4, pretrained=False, pretrained_model_path='', learning_rate=1e-6, metrics=[dice]): if n_classes == 1: loss = 'binary_crossentropy' final_act = 'sigmoid' elif n_classes > 1: loss = 'categorical_crossentropy' final_act = 'softmax' if pretrained: model = load_model(pretrained_model_path, custom_objects={ 'dice': dice, 'preprocess_input': preprocess_input, '_preprocess_symbolic_input': _preprocess_symbolic_input }) model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate), loss=loss, metrics=metrics) model.summary() return model i = Input(shape=INPUT_SHAPE) converted_inputs = tf.keras.layers.Lambda( lambda x: preprocess_input(x, mode='torch'))(i) ## Block 1 x = Conv2D(2**base, (3, 3), activation='relu', padding='same', name='block1_conv1')(converted_inputs) x = Dropout(0.1)(x) x = Conv2D(2**base, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) f1 = x # Block 2 x = Conv2D(2**(base + 1), (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Dropout(0.1)(x) x = Conv2D(2**(base + 1), (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) f2 = x # Block 3 x = Conv2D(2**(base + 2), (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Dropout(0.2)(x) x = Conv2D(2**(base + 2), (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(2**(base + 2), (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) pool3 = x # Block 4 x = Conv2D(2**(base + 3), (3, 3), activation='relu', padding='same', name='block4_conv1')(pool3) x = Dropout(0.2)(x) x = Conv2D(2**(base + 3), (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(2**(base + 3), (3, 3), activation='relu', padding='same', name='block4_conv3')(x) pool4 = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Conv2D(2**(base + 3), (3, 3), activation='relu', padding='same', name='block5_conv1')(pool4) x = Dropout(0.2)(x) x = Conv2D(2**(base + 3), (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(2**(base + 3), (3, 3), activation='relu', padding='same', name='block5_conv3')(x) pool5 = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) conv6 = Conv2D(4096, (7, 7), activation='relu', padding='same', name="conv6")(pool5) conv6 = Dropout(0.5)(conv6) conv7 = Conv2D(4096, (1, 1), activation='relu', padding='same', name="conv7")(conv6) conv7 = Dropout(0.5)(conv7) pool4_n = Conv2D(n_classes, (1, 1), activation='relu', padding='same')(pool4) u2 = Conv2DTranspose(n_classes, kernel_size=(2, 2), strides=(2, 2), padding='same')(conv7) u2_skip = Add()([pool4_n, u2]) pool3_n = Conv2D(n_classes, (1, 1), activation='relu', padding='same')(pool3) u4 = Conv2DTranspose(n_classes, kernel_size=(2, 2), strides=(2, 2), padding='same')(u2_skip) u4_skip = Add()([pool3_n, u4]) o = Conv2DTranspose(n_classes, kernel_size=(8, 8), strides=(8, 8), padding='same', activation=final_act)(u4_skip) model = Model(inputs=i, outputs=o, name='fcn8') model.compile( optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate), loss=loss, metrics=metrics) model.summary() return model
def create(n_classes=1, base=2, pretrained=False, pretrained_model_path='', learning_rate=1e-6, metrics=[dice]): if n_classes == 1: loss = 'binary_crossentropy' final_act = 'sigmoid' elif n_classes > 1: loss = 'categorical_crossentropy' final_act = 'softmax' if pretrained: model = load_model(pretrained_model_path, custom_objects={ 'dice': dice, 'preprocess_input': preprocess_input, '_preprocess_symbolic_input': _preprocess_symbolic_input }) model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate), loss=loss, metrics=metrics) model.summary() return model inputs = tf.keras.layers.Input(INPUT_SHAPE) b = base converted_inputs = tf.keras.layers.Lambda( lambda x: preprocess_input(x, mode='torch'))(inputs) conv_1 = tf.keras.layers.Conv2D(2**b, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(converted_inputs) conv_1 = tf.keras.layers.Dropout(0.2)(conv_1) conv_1 = tf.keras.layers.Conv2D(2**b, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_1) pool_1 = tf.keras.layers.MaxPooling2D((2, 2))(conv_1) conv_2 = tf.keras.layers.Conv2D(2**(b + 1), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool_1) conv_2 = tf.keras.layers.Dropout(0.2)(conv_2) conv_2 = tf.keras.layers.Conv2D(2**(b + 1), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_2) pool_2 = tf.keras.layers.MaxPooling2D((2, 2))(conv_2) conv_3 = tf.keras.layers.Conv2D(2**(b + 2), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool_2) conv_3 = tf.keras.layers.Dropout(0.2)(conv_3) conv_3 = tf.keras.layers.Conv2D(2**(b + 2), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_3) pool_3 = tf.keras.layers.MaxPooling2D((2, 2))(conv_3) conv_4 = tf.keras.layers.Conv2D(2**(b + 3), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool_3) conv_4 = tf.keras.layers.Dropout(0.2)(conv_4) conv_4 = tf.keras.layers.Conv2D(2**(b + 3), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_4) pool_4 = tf.keras.layers.MaxPooling2D((2, 2))(conv_4) conv_5 = tf.keras.layers.Conv2D(2**(b + 4), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool_4) conv_5 = tf.keras.layers.Dropout(0.2)(conv_5) conv_5 = tf.keras.layers.Conv2D(2**(b + 4), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_5) pool_5 = tf.keras.layers.MaxPooling2D((2, 2))(conv_5) conv_6 = tf.keras.layers.Conv2D(2**(b + 5), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool_5) conv_6 = tf.keras.layers.Dropout(0.1)(conv_6) conv_6 = tf.keras.layers.Conv2D(2**(b + 5), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_6) pool_6 = tf.keras.layers.MaxPooling2D((2, 2))(conv_6) conv_7 = tf.keras.layers.Conv2D(2**(b + 6), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool_6) conv_7 = tf.keras.layers.Dropout(0.1)(conv_7) conv_7 = tf.keras.layers.Conv2D(2**(b + 6), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_7) de_conv_1 = tf.keras.layers.Conv2DTranspose(2**(b + 5), (2, 2), strides=(2, 2), padding='same')(conv_7) de_conv_1 = tf.keras.layers.concatenate([de_conv_1, conv_6]) conv_8 = tf.keras.layers.Conv2D(2**(b + 5), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(de_conv_1) conv_8 = tf.keras.layers.Dropout(0.1)(conv_8) conv_8 = tf.keras.layers.Conv2D(2**(b + 5), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_8) de_conv_2 = tf.keras.layers.Conv2DTranspose(2**(b + 4), (2, 2), strides=(2, 2), padding='same')(conv_8) de_conv_2 = tf.keras.layers.concatenate([de_conv_2, conv_5]) conv_9 = tf.keras.layers.Conv2D(2**(b + 4), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(de_conv_2) conv_9 = tf.keras.layers.Dropout(0.1)(conv_9) conv_9 = tf.keras.layers.Conv2D(2**(b + 4), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_9) de_conv_3 = tf.keras.layers.Conv2DTranspose(2**(b + 3), (2, 2), strides=(2, 2), padding='same')(conv_9) de_conv_3 = tf.keras.layers.concatenate([de_conv_3, conv_4]) conv_10 = tf.keras.layers.Conv2D(2**(b + 3), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(de_conv_3) conv_10 = tf.keras.layers.Dropout(0.1)(conv_10) conv_10 = tf.keras.layers.Conv2D(2**(b + 3), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_10) de_conv_4 = tf.keras.layers.Conv2DTranspose(2**(b + 2), (2, 2), strides=(2, 2), padding='same')(conv_10) de_conv_4 = tf.keras.layers.concatenate([de_conv_4, conv_3]) conv_11 = tf.keras.layers.Conv2D(2**(b + 2), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(de_conv_4) conv_11 = tf.keras.layers.Dropout(0.1)(conv_11) conv_11 = tf.keras.layers.Conv2D(2**(b + 2), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_11) de_conv_5 = tf.keras.layers.Conv2DTranspose(2**(b + 1), (2, 2), strides=(2, 2), padding='same')(conv_11) de_conv_5 = tf.keras.layers.concatenate([de_conv_5, conv_2]) conv_12 = tf.keras.layers.Conv2D(2**(b + 1), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(de_conv_5) conv_12 = tf.keras.layers.Dropout(0.1)(conv_12) conv_12 = tf.keras.layers.Conv2D(2**(b + 1), (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_12) de_conv_6 = tf.keras.layers.Conv2DTranspose(2**b, (2, 2), strides=(2, 2), padding='same')(conv_12) de_conv_6 = tf.keras.layers.concatenate([de_conv_6, conv_1]) conv_13 = tf.keras.layers.Conv2D(2**b, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(de_conv_6) conv_13 = tf.keras.layers.Dropout(0.1)(conv_13) conv_13 = tf.keras.layers.Conv2D(2**b, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv_13) outputs = tf.keras.layers.Conv2D(n_classes, (1, 1), activation=final_act)(conv_13) model = tf.keras.Model(inputs=[inputs], outputs=[outputs]) model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate), loss=loss, metrics=metrics) model.summary() return model
batchImages = [] # loop over the images and labels in the current batch for (j, imagePath) in enumerate(batchPaths): # load the input image using the Keras helper utility # while ensuring the image is resized to 224x224 pixels print(i, '-------------------------', j) print(len(imagePath)) image = load_img(imagePath, target_size=(224, 224)) image = img_to_array(image) # preprocess the image by (1) expanding the dimensions and # (2) subtracting the mean RGB pixel intensity from the # ImageNet dataset image = np.expand_dims(image, axis=0) image = imagenet_utils.preprocess_input(image) # add the image to the batch batchImages.append(image) # pass the images through the network and use the outputs as # our actual features batchImages = np.vstack(batchImages) features = model.predict(batchImages, batch_size=bs) # reshape the features so that each image is represented by # a flattened feature vector of the ‘MaxPooling2D‘ outputs features = features.reshape((features.shape[0], 512 * 7 * 7)) # add the features and labels to our HDF5 dataset dataset.add(features, batchLabels)
def extract_one_image_feature(image): numpy_image = img_to_array(image) image_batch = np.expand_dims(numpy_image, axis=0) print('image batch size', image_batch.shape) preprocessed_image = preprocess_input(image_batch.copy()) return preprocessed_image
def preprocess_input(x, data_format=None): """Preprocesses the input (encoding a batch of images) for the model.""" return imagenet_utils.preprocess_input(x, data_format=data_format, mode='tf')
def preprocess_input(x, data_format=None): return imagenet_utils.preprocess_input(x, data_format=data_format, mode='tf')
def create(n_classes=1, base=4, pretrained=False, pretrained_model_path='', learning_rate=1e-6, metrics=[dice]): if n_classes == 1: loss = 'binary_crossentropy' final_act = 'sigmoid' elif n_classes > 1: loss = 'categorical_crossentropy' final_act = 'softmax' if pretrained: model = load_model(pretrained_model_path, custom_objects={ 'dice': dice, 'preprocess_input': preprocess_input, '_preprocess_symbolic_input': _preprocess_symbolic_input }) model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate), loss=loss, metrics=metrics) model.summary() return model b = base inputs = Input(shape=INPUT_SHAPE) converted_inputs = tf.keras.layers.Lambda( lambda x: preprocess_input(x, mode='torch'))(inputs) x = Conv2D(2**(b + 1), (3, 3), strides=(2, 2), name='conv_1_1', use_bias=False, padding='same')(converted_inputs) x = BatchNormalization(name='conv_1_1_batch_normalization')(x) x = Activation('relu')(x) x = Conv2D(2**(b + 2), (3, 3), strides=(1, 1), padding='same', use_bias=False, dilation_rate=(1, 1), name='conv_1_2')(x) x = BatchNormalization(name='conv_1_2_batch_normalization')(x) x = Activation('relu')(x) x = xception_block(x, [128, 128, 128], 'xception_block_1', skip_type='conv', stride=2, depth_activation=False) x, skip1 = xception_block(x, [256, 256, 256], 'xception_block_2', skip_type='conv', stride=2, depth_activation=False, return_skip=True) x = xception_block(x, [728, 728, 728], 'xception_block_3', skip_type='conv', stride=1, depth_activation=False) for i in range(16): x = xception_block(x, [728, 728, 728], 'middle_flow_unit_{}'.format(i + 1), skip_type='sum', stride=1, rate=2, depth_activation=False) x = xception_block(x, [728, 1024, 1024], 'xception_block_4', skip_type='conv', stride=1, rate=2, depth_activation=False) x = xception_block(x, [1536, 1536, 2048], 'xception_block_5', skip_type='none', stride=1, rate=4, depth_activation=True) b4 = GlobalAveragePooling2D()(x) b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4) b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4) b4 = Conv2D(2**(b + 4), (1, 1), padding='same', use_bias=False, name='image_pooling')(b4) b4 = BatchNormalization(name='image_pooling_batch_normalization', epsilon=1e-5)(b4) b4 = Activation('relu')(b4) size_before = int_shape(x) b4 = Lambda(lambda x: tf.compat.v1.image.resize( x, size_before[1:3], method='bilinear', align_corners=True))(b4) b0 = Conv2D(2**(b + 4), (1, 1), padding='same', use_bias=False, name='atrous_spatial_pyramid_pooling_base')(x) b0 = BatchNormalization( name='atrous_spatial_pyramid_pooling_base_batch_normalization', epsilon=1e-5)(b0) b0 = Activation('relu', name='atrous_spatial_pyramid_pooling_base_activation')(b0) b1 = separable_conv_with_batch_normalization( x, 2**(b + 4), 'atrous_spatial_pyramid_pooling_1', rate=12) b2 = separable_conv_with_batch_normalization( x, 2**(b + 4), 'atrous_spatial_pyramid_pooling_2', rate=24) b3 = separable_conv_with_batch_normalization( x, 2**(b + 4), 'atrous_spatial_pyramid_pooling_3', rate=36) x = Concatenate()([b4, b0, b1, b2, b3]) x = Conv2D(2**(b + 4), (1, 1), padding='same', use_bias=False, name='concat_projection')(x) x = BatchNormalization(name='concat_projection_batch_normalization', epsilon=1e-5)(x) x = Activation('relu')(x) x = Dropout(0.1)(x) skip_size = int_shape(skip1) x = Lambda(lambda xx: tf.compat.v1.image.resize( xx, skip_size[1:3], method='bilinear', align_corners=True))(x) dec_skip1 = Conv2D(48, (1, 1), padding='same', use_bias=False, name='feature_projection0')(skip1) dec_skip1 = BatchNormalization( name='feature_projection0_batch_normalization', epsilon=1e-5)(dec_skip1) dec_skip1 = Activation('relu')(dec_skip1) x = Concatenate()([x, dec_skip1]) x = separable_conv_with_batch_normalization(x, 2**(b + 4), 'decoder_convolution_1') x = separable_conv_with_batch_normalization(x, 2**(b + 4), 'decoder_convolution_2') x = Conv2D(n_classes, (1, 1), padding='same', name="last_layer")(x) size_before3 = int_shape(inputs) x = Lambda(lambda xx: tf.compat.v1.image.resize( xx, size_before3[1:3], method='bilinear', align_corners=True))(x) outputs = tf.keras.layers.Activation(final_act)(x) model = Model(inputs, outputs, name='deeplabv3plus') model.compile( optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate), loss=loss, metrics=metrics) model.summary() return model