def getOneStatic(imPath, sampSize, newSize): ''' randomly samples an image with no transformations ''' im = image.load_img(imPath).resize((newSize, newSize)) r = int(np.random.rand() * (newSize - sampSize)) c = int(np.random.rand() * (newSize - sampSize)) im = im.crop((r, c, r + sampSize, c + sampSize)) return image.img_to_array(im) / 255.
def getImage(sampSize, newSize, imPath, _=''): ''' randomly samples an image ''' f1 = int(np.sign(np.random.rand() - .5)) f2 = int(np.sign(np.random.rand() - .5)) im = image.load_img(imPath).resize((newSize, newSize)) r = int(np.random.rand() * (newSize - sampSize)) c = int(np.random.rand() * (newSize - sampSize)) im = im.crop((r, c, r + sampSize, c + sampSize)) im = image.random_rotation(im, 5) im = image.img_to_array(im)[:, ::-f1, ::-f2] im = (im - im.min()) / (im.max() - im.min()) return im
def getOne(batchSize, imPath): allIms = np.zeros((batchSize, 3, sampSize, sampSize)) for i in xrange(batchSize): f1 = int(np.sign(np.random.rand() - .5)) f2 = int(np.sign(np.random.rand() - .5)) im = image.load_img(imPath).resize((newSize, newSize)) r = int(np.random.rand() * (newSize - sampSize)) c = int(np.random.rand() * (newSize - sampSize)) im = im.crop((r, c, r + sampSize, c + sampSize)) im = image.random_rotation(im, 5) allIms[i, :, :, :] = image.img_to_array(im) allIms[i, :, :, :] = allIms[i, :, ::-f1, ::-f2] / 255. return allIms
def Gamma_correction(path, image_save_dir, aug_number): img = load_img(path, grayscale=True) # 这是一个PIL图像 x = img_to_array(img) # 把PIL图像转换成一个numpy数组 x = x.reshape(x.shape[0], -1) # 转成二维数组 for i in range(aug_number): gamma = np.random.uniform(low=0.7, high=1.3) adjusted_img = (x / 255)**gamma * 255 image_name = os.path.basename(path) prefix = image_name.split(".")[0] save_path = os.path.join(image_save_dir, prefix + '_aug_gamma_' + str(i) + '.jpg') scipy.misc.imsave(save_path, adjusted_img)
def get_img_list(file_path, file_list, img_type, data_augment=False): from image import load_img tmp_list = list() for filename in file_list: img = load_img(os.path.join(file_path, filename), grayscale=True, target_size=(img_rows, img_cols)) tmp_list.append(img) if data_augment: tmp_list += data_augmentation(img, img_type=img_type) return tmp_list
def getBatch(batchSize, path): allPaths = image.list_pictures(path) allIms = np.zeros((batchSize, 3, sampSize, sampSize)) for i in xrange(batchSize): f1 = int(np.sign(np.random.rand() - .5)) f2 = int(np.sign(np.random.rand() - .5)) im = image.load_img(allPaths[int(np.random.rand() * len(allPaths))]).resize((newSize, newSize)) r = int(np.random.rand() * (newSize - sampSize)) c = int(np.random.rand() * (newSize - sampSize)) im = im.crop((r, c, r + sampSize, c + sampSize)) im = image.random_rotation(im, 5) allIms[i, :, :, :] = image.img_to_array(im) allIms[i, :, :, :] = allIms[i, :, ::-f1, ::-f2] return allIms/255.
def process_image(self, img_path, show=False): img = image.load_img(img_path, target_size=(150, 150)) img_tensor = image.img_to_array(img) # (height, width, channels) img_tensor = np.expand_dims( img_tensor, axis=0 ) # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels) img_tensor /= 255. # imshow expects values in the range [0, 1] if show: plt.imshow(img_tensor[0]) plt.axis('off') plt.show() return self.model.predict(img_tensor)
def Noise_Injection(path, image_save_dir, aug_number): img = load_img(path, grayscale=True) # 这是一个PIL图像 x = img_to_array(img) # 把PIL图像转换成一个numpy数组 x = x.reshape(x.shape[0], -1) # 转成二维数组 x_shape = x.shape for i in range(aug_number): noise = np.random.normal(loc=0.0, scale=0.01, size=x_shape) # noise=np.random.random(size=x_shape) x_add_noise = np.add(x, noise) # 保存图片 image_dir = os.path.dirname(path) image_name = os.path.basename(path) prefix = image_name.split(".")[0] save_path = os.path.join(image_save_dir, prefix + '_aug_noise_' + str(i) + '.jpg') scipy.misc.imsave(save_path, x_add_noise)
def Scaling(path, image_save_dir, aug_number): datagen = ImageDataGenerator(zoom_range=0.3, fill_mode='constant') # 0.3 img = load_img(path, grayscale=True) # 这是一个PIL图像 x = img_to_array(img) # 把PIL图像转换成一个numpy数组 x = x.reshape((1, ) + x.shape) # 这是一个numpy数组 image_dir = os.path.dirname(path) image_name = os.path.basename(path) prefix = image_name.split(".")[0] i = 1 for batch in datagen.flow(x, batch_size=1, save_to_dir=image_save_dir, save_prefix=prefix + "_aug_" + 'Scal', save_format='jpg'): i += 1 if i > aug_number: break # 否则生成器会退出循环
def Random_translation(path, image_save_dir, aug_number): datagen = ImageDataGenerator(width_shift_range=0.05, height_shift_range=0.05) # 0.2 img = load_img(path, grayscale=True) # 这是一个PIL图像 x = img_to_array(img) # 把PIL图像转换成一个numpy数组 x = x.reshape((1, ) + x.shape) # 这是一个numpy数组 image_dir = os.path.dirname(path) image_name = os.path.basename(path) prefix = image_name.split(".")[0] i = 1 for batch in datagen.flow(x, batch_size=1, save_to_dir=image_save_dir, save_prefix=prefix + "_aug_" + 'trans', save_format='jpg'): i += 1 if i > aug_number: break # 否则生成器会退出循环
def Random_affine_transform(path, image_save_dir, aug_number): datagen = ImageDataGenerator( shear_range=3) # 水平或垂直投影变换,shear_range是角度范围 #5 img = load_img(path, grayscale=True) # 这是一个PIL图像 x = img_to_array(img) # 把PIL图像转换成一个numpy数组 x = x.reshape((1, ) + x.shape) # 这是一个numpy数组 image_dir = os.path.dirname(path) image_name = os.path.basename(path) prefix = image_name.split(".")[0] i = 1 for batch in datagen.flow(x, batch_size=1, save_to_dir=image_save_dir, save_prefix=prefix + "_aug_" + 'aff', save_format='jpg'): i += 1 if i > aug_number: break # 否则生成器会退出循环
def getBatch(batchSize, allPaths, numClass): allIms = np.zeros((batchSize, 3, sampSize, sampSize)) Y = np.zeros((batchSize, 1)).astype('int') rIms = np.zeros((batchSize, 1)).astype('str') for i in xrange(batchSize): f1 = int(np.sign(np.random.rand() - .5)) f2 = int(np.sign(np.random.rand() - .5)) rIm = allPaths[int(np.random.rand() * len(allPaths))] rIms[i] = rIm im = image.load_img(rIm).resize((newSize, newSize)) Y[i] = labels[rIm.split('/')[-1]] r = int(np.random.rand() * (newSize - sampSize)) c = int(np.random.rand() * (newSize - sampSize)) im = im.crop((r, c, r + sampSize, c + sampSize)) im = image.random_rotation(im, 5) allIms[i, :, :, :] = image.img_to_array(im) allIms[i, :, :, :] = allIms[i, :, ::-f1, ::-f2] if numClass > 2: Y2 = np.zeros((batchSize, numClass)).astype('int') for i in xrange(batchSize): Y2[i, Y[i][0]] = 1 Y = Y2 return allIms, Y, rIms
def post(self): image_data = self.request.files['image'][0] np_arr = np.fromstring(image_data['body'], np.uint8) image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) image_path = '/tmp/%s.png' % str(uuid.uuid4()) cv2.imwrite(image_path, image_np) image_f = image.load_img(image_path, target_size=(64, 64)) image_f = image.img_to_array(image_f) image_f = np.expand_dims(image_f, axis=0) left_or_right = self.application.model['left_or_right'] straight_layer = self.application.model['straight'] left_or_right_result = left_or_right.predict(image_f) straight_layer_result = straight_layer.predict(image_f) os.remove(image_path) self.write( json.dumps({ 'left_or_right': left_or_right_result, 'straight': straight_layer_result }))
import tensorflow as tf ITERATIONS = 1000 LEARNING_RATE = 5.0 ALPHA = 10000 BETA = 1 STYLE = 'img/style/matisse.jpg' CONTENT = 'img/content/sunflower.jpg' if __name__ == '__main__': with tf.Session() as sess: # Load images content = load_img(CONTENT) style = load_img(STYLE) # Using content as base instead of white noise input = content model = create_model() # Content loss sess.run(tf.global_variables_initializer()) sess.run(model['input'].assign(content)) L_content = content_loss(sess, model) # Style loss sess.run(tf.global_variables_initializer()) sess.run(model['input'].assign(style))
def read_img_test(filepath, size): img = T.load_img(os.path.join(test_dir, filepath), target_size=size) img = T.img_to_array(img) return img