def main(): tf.enable_eager_execution() model = get_model() style_features_group, content_features = get_feature_representations(model) if use_sliding_window: combined_array = content_array for i in range(iteration_size): if i % 20 == 0: print(i) image.save_img(x=image.array_to_img(deprocess_image(np.array(combined_array))), path='./combined/combine%d.jpg' % i) for row in range(0, content_array.shape[0], window_stride): for col in range(0, content_array.shape[1], window_stride): part_array = np.expand_dims(combined_array[row: row + window_size, col: col + window_size], axis=0) part_array = tf.Variable(part_array) gradient_decent(model, part_array, style_features_group, content_features) combined_array[row: row + window_size, col: col + window_size] = part_array.numpy()[0] else: combined_array = tf.Variable(np.expand_dims(content_array, axis=0), dtype=tf.float32) for i in range(iteration_size): if i % 50 == 0: print(i) image.save_img(x=image.array_to_img(deprocess_image(combined_array.numpy()[0])), path='./combined/combine%d.jpg' % i) gradient_decent(model, combined_array, style_features_group, content_features)
def plot_predict(self, batch_input, batch_pred, batch_teach: '4dim_array', save_folder='train', batch_num=1) -> 'im': for i in range(batch_input.shape[0]): os.makedirs(os.path.join(self.main_dir, save_folder), exist_ok=True) #one_hot->grayscaleに変換している。 pred = np.argmax(batch_pred[i, :, :, :], axis=2) teach = np.argmax(batch_teach[i, :, :, :], axis=2) seg = np.hstack((pred, teach)) seg = np.where(seg == 1, 128, seg) seg = np.where(seg == 2, 255, seg) #3次元に展開 # seg=seg[:,:,np.newaxis] seg_im = seg.astype(np.uint8) seg_im = Image.fromarray(seg_im, mode='L') vol = batch_input[i, :, :, :] vol = 255 * vol / (np.max(vol) - np.min(vol)) vol_im = vol.astype(np.uint8) array_to_img(vol_im).save( os.path.join( self.main_dir, save_folder, f'input_{self.parser.batch_size*batch_num+i}.png')) seg_im.save( os.path.join(self.main_dir, save_folder, f'pred_{self.parser.batch_size*batch_num+i}.png'))
def test_img_utils(self): if PIL is None: return # Skip test if PIL is not available. height, width = 10, 8 # Test channels_first data format x = np.random.random((3, height, width)) img = preprocessing_image.array_to_img( x, data_format='channels_first') self.assertEqual(img.size, (width, height)) x = preprocessing_image.img_to_array( img, data_format='channels_first') self.assertEqual(x.shape, (3, height, width)) # Test 2D x = np.random.random((1, height, width)) img = preprocessing_image.array_to_img( x, data_format='channels_first') self.assertEqual(img.size, (width, height)) x = preprocessing_image.img_to_array( img, data_format='channels_first') self.assertEqual(x.shape, (1, height, width)) # Test channels_last data format x = np.random.random((height, width, 3)) img = preprocessing_image.array_to_img(x, data_format='channels_last') self.assertEqual(img.size, (width, height)) x = preprocessing_image.img_to_array(img, data_format='channels_last') self.assertEqual(x.shape, (height, width, 3)) # Test 2D x = np.random.random((height, width, 1)) img = preprocessing_image.array_to_img(x, data_format='channels_last') self.assertEqual(img.size, (width, height)) x = preprocessing_image.img_to_array(img, data_format='channels_last') self.assertEqual(x.shape, (height, width, 1))
def _get_batches_of_transformed_samples(self, index_array): batch_x = np.zeros(tuple([len(index_array)] + list(self.x.shape)[1:])) batch_y = [] for y_sem in self.y_semantic_list: shape = tuple([len(index_array)] + list(y_sem.shape[1:])) batch_y.append(np.zeros(shape, dtype=y_sem.dtype)) for i, j in enumerate(index_array): x = self.x[j] y_semantic_list = [y_sem[j] for y_sem in self.y_semantic_list] # Apply transformation x, y_semantic_list = self.image_data_generator.random_transform( x, y_semantic_list) x = self.image_data_generator.standardize(x) batch_x[i] = x for k, y_sem in enumerate(y_semantic_list): batch_y[k][i] = y_sem if self.save_to_dir: for i, j in enumerate(index_array): if self.data_format == 'channels_first': img_x = np.expand_dims(batch_x[i, 0, ...], 0) else: img_x = np.expand_dims(batch_x[i, ..., 0], -1) img = array_to_img(img_x, self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e4), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) if self.y is not None: # Save argmax of y batch for k, y_sem in enumerate(batch_y): if y_sem[i].shape[self.channel_axis - 1] == 1: img_y = y_sem[i] else: img_y = np.argmax(y_sem[i], axis=self.channel_axis - 1) img_y = np.expand_dims(img_y, axis=self.channel_axis - 1) img = array_to_img(img_y, self.data_format, scale=True) fname = 'y_{sem}_{prefix}_{index}_{hash}.{format}'.format( sem=k, prefix=self.save_prefix, index=j, hash=np.random.randint(1e4), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) return batch_x, batch_y
def save_imgs(path, imgs, rows, cols): base_width = imgs.shape[1] base_height = imgs.shape[2] channels = imgs.shape[3] output_shape = (base_height * rows, base_width * cols, channels) buffer = np.zeros(output_shape) for row in range(rows): for col in range(cols): img = imgs[row * cols + col] buffer[row * base_height:(row + 1) * base_height, col * base_width:(col + 1) * base_width] = img array_to_img(buffer).save(path)
def _get_batches_of_transformed_samples(self, index_array): batch_x = np.zeros(tuple([len(index_array)] + list(self.x.shape)[1:])) batch_y = np.zeros(tuple([len(index_array)] + list(self.y.shape)[1:])) for i, j in enumerate(index_array): x = self.x[j] if self.y is not None: y = self.y[j] x, y = self.image_data_generator.random_transform( x.astype(K.floatx()), y) else: x = self.image_data_generator.random_transform( x.astype(K.floatx())) x = self.image_data_generator.standardize(x) batch_x[i] = x batch_y[i] = y if self.save_to_dir: for i, j in enumerate(index_array): if self.data_format == 'channels_first': img_x = np.expand_dims(batch_x[i, 0, ...], 0) else: img_x = np.expand_dims(batch_x[i, ..., 0], -1) img = array_to_img(img_x, self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e4), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) if self.y is not None: # Save argmax of y batch img_y = np.argmax(batch_y[i], axis=self.channel_axis - 1) img_y = np.expand_dims(img_y, axis=self.channel_axis - 1) img = array_to_img(img_y, self.data_format, scale=True) fname = 'y_{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e4), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) if self.y is None: return batch_x if self.skip is not None: batch_y = [batch_y] * (self.skip + 1) return batch_x, batch_y
def _get_batches_of_transformed_samples(self, index_array): """For python 2.x. # Returns The next batch. """ # The transformation of images is not under thread lock # so it can be done in parallel batch_x = np.zeros(tuple([len(index_array)] + list(self.x.shape)[1:]), dtype=self.dtype) for i, j in enumerate(index_array): x = self.x[j] x = self.image_data_generator.random_transform(x.astype( self.dtype)) x = self.image_data_generator.standardize(x) batch_x[i] = x if self.save_to_dir: for i, j in enumerate(index_array): img = array_to_img(batch_x[i], self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e4), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) if self.y is None: return batch_x batch_y = self.y[index_array] return batch_x, batch_y
def augment_category(self, category, aug_count=10): self.logger.info('Augmenting images in category {}'.format(category)) # create the input path for category category_dir = os.path.join(self.current_working_dir, constants.OUTPUT_DATA_DIR, category) # searches files in category dir list_category_dir = os.listdir(category_dir) list_category_dir = natsorted(list_category_dir) # loops through all files in path for img in tqdm(list_category_dir): if '.jpg' in img or '.png' in img: image_path = os.path.join(category_dir, img) # load image to array image = load_img(image_path) if image is not None: # convert image to array image = img_to_array(image) # reshape to array rank 4 image = image.reshape((1,) + image.shape) # create infinite flow of images images_flow = self.image_data_gen.flow(image, batch_size=1) for i, new_images in enumerate(images_flow): # we access only first image because of batch_size=1 new_image = array_to_img(new_images[0], scale=True) # save the augmented image image_name = '{:s}_{:s}_aug.jpg'.format(img, str(i)) image_path = os.path.join(category_dir, image_name) new_image.save(image_path) # break infinite loop after generated 10 images if i >= aug_count: break
def predict(self, IMG): # pImage = open(IMG, 'rb').read() # tensorIMG = tf.image.decode_jpeg(pImage) pIMG = image.array_to_img(IMG)# .resize((256, 144)) tensorIMG = image.img_to_array(pIMG) x = np.array(tensorIMG / 255.0) # show image iColumn = 4 # generate window plt.figure(num='Input') # plt.subplot(1, 1, 1) plt.imshow(x) # imagetest = x x = np.expand_dims(x, axis=0) # pyplot.imshow(x) time1 = datetime.datetime.now() outputs = self.__pVisualModel.predict(x) time2 = datetime.datetime.now() print(time2 - time1) i = 100 listOutput = [] for i in range(len(outputs)): outputShape = outputs[i].shape singleOut = outputs[i].reshape(outputShape[1], outputShape[2], outputShape[3]) # singleOut *= 255 listOutput.append(singleOut) singleOut = listOutput[-1] singleOut[singleOut > 0.5] = 1 listOutput[-1] = singleOut return listOutput '''
def resize(item: np.ndarray, height: int, width: int, keep_ratio: bool = False) -> np.ndarray: """ Parameters ---------- item Input array height Target height width Target width keep_ratio Keep image aspect ratio Returns ------- np.ndarray Resized image in numpy array """ img = array_to_img(item, scale=False) if keep_ratio: img.thumbnail((width, width), Image.ANTIALIAS) resized_img = img else: resized_img = img.resize((width, height), resample=Image.NEAREST) resized_img = img_to_array(resized_img) resized_img = resized_img.astype(dtype=np.uint8) return resized_img
def _generate_test_images(): img_w = img_h = 20 rgb_images = [] gray_images = [] for _ in range(8): bias = np.random.rand(img_w, img_h, 1) * 64 variance = np.random.rand(img_w, img_h, 1) * (255 - 64) imarray = np.random.rand(img_w, img_h, 3) * variance + bias im = preprocessing_image.array_to_img(imarray, scale=False) rgb_images.append(im) imarray = np.random.rand(img_w, img_h, 1) * variance + bias im = preprocessing_image.array_to_img(imarray, scale=False) gray_images.append(im) return [rgb_images, gray_images]
def _get_batches_of_transformed_samples(self, index_array): batch_x = np.zeros(tuple([len(index_array)] + list(self.x.shape)[1:]), dtype=K.floatx()) batch_coords = [self.coords[i] for i in index_array] x, y = self.func_patch(self._x, self._y, batch_coords, self.patch_h, self.patch_w) self.x = x self.y = y index_array = np.arange(len(self.y)) for i, j in enumerate(index_array): x = self.x[j] x = self.image_data_generator.random_transform(x.astype( K.floatx())) x = self.image_data_generator.standardize(x) batch_x[i] = x if self.save_to_dir: for i, j in enumerate(index_array): img = array_to_img(batch_x[i], self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e4), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) if self.y is None: return batch_x batch_y = self.y[index_array] return batch_x, batch_y
def save_img(): imgs = np.load(Test_PATH + '/test_result.npy') for i in range(imgs.shape[0]): img = imgs[i] img = array_to_img(img) img.save(Test_PATH + '/result%d.png' % (i)) return
def _write_image(filepath, img_w=30, img_h=30): bias = np.random.rand(img_w, img_h, 1) * 64 variance = np.random.rand(img_w, img_h, 1) * (255 - 64) imarray = np.random.rand(img_w, img_h, 1) * variance + bias if filepath.lower().endswith('tif') or filepath.lower().endswith('tiff'): tiff.imsave(filepath, imarray[:, :, 0]) else: img = array_to_img(imarray, scale=False, data_format='channels_last') img.save(filepath)
def predict(latent_dim, height, width, channels): random_latent_vectors = np.random.normal(size=(100, latent_dim)) dcgan = DCGAN(latent_dim, height, width, channels) dcgan.load_weights('gan_epoch20.h5') # dcgan.load_weights('gan_epoch1.h5') generated_images = dcgan.predict(random_latent_vectors) for i, generated_image in enumerate(generated_images): img = image.array_to_img(denormalize(generated_image), scale=False) img.save(os.path.join('generated', str(i) + '.png'))
def test(covert_model, images_list, input_shape=(224, 224, 3)): # 変換 predict = covert_model.predict(images_list) for i in range(predict.shape[0]): # 保存できる画像に変換 predict_image = array_to_img(predict[i]) # 保存 file_name = f'{test_files}/predicted_images/test{i}.jpg' predict_image.save(file_name) print(f'>> predict! test{i}.jpg')
def test(covert_model, step, now, input_shape=(224, 224, 3)): # テスト画像読み込み test_image = load_img(TEST_IMAGE, target_size=input_shape[:2]) # 入力用に変換 test_image = np.expand_dims(img_to_array(test_image), axis=0) # 変換 predict = covert_model.predict(test_image) # 保存できる画像に変換 predict_image = array_to_img(predict[0]) # 保存 predict_image.save('./img/test/{}/predicted_step{}.jpg'.format(now, step))
def save_tsne_grid(img_collection, labels, activations, out_res, out_dim, out_dir, tsne_iter=5000, out_name='tsne_visualization.png', quality=75, subsampling=2, border=10): print('entering tsne function') X_2d = generate_tsne(activations, tsne_iter) grid = np.dstack( np.meshgrid(np.linspace(0, 1, out_dim), np.linspace(0, 1, out_dim))).reshape(-1, 2) cost_matrix = cdist(grid, X_2d, "sqeuclidean").astype(np.float32) print(cost_matrix.shape) cost_matrix = cost_matrix * (100000 / cost_matrix.max()) row_asses, col_asses, _ = lapjv(cost_matrix) grid_jv = grid[col_asses] out = np.ones((out_dim * (out_res + 2 * border), out_dim * (out_res + 2 * border), 3)) print(out.shape) fnt = ImageFont.load_default() fnt.size = 40 for pos, img, l in zip(grid_jv, img_collection, labels): h_range = int(np.floor(pos[0] * (out_dim - 1) * (out_res + 2 * border))) w_range = int(np.floor(pos[1] * (out_dim - 1) * (out_res + 2 * border))) img = Image.fromarray(np.uint8(((img + 1) / 2) * 255)) img_with_border = ImageOps.expand(img, border=border, fill=COLORS[l % len(COLORS)]) d = ImageDraw.Draw(img_with_border) d.text((10, 10), str(l), font=fnt, fill=COLORS[l % len(COLORS)]) out[h_range:h_range + (out_res + 2 * border), w_range:w_range + (out_res + 2 * border)] = img_with_border print(out.shape) im = image.array_to_img(out) im.save(os.path.join(out_dir, 'full_' + out_name), quality=quality, subsampling=subsampling) im = im.resize((1024, 1024)) im.save(os.path.join(out_dir, out_name), quality=quality, subsampling=subsampling)
def _get_images(self, count=16, color_mode='rgb'): width = height = 24 imgs = [] for _ in range(count): if color_mode == 'grayscale': img = np.random.randint(0, 256, size=(height, width, 1)) elif color_mode == 'rgba': img = np.random.randint(0, 256, size=(height, width, 4)) else: img = np.random.randint(0, 256, size=(height, width, 3)) img = image_preproc.array_to_img(img) imgs.append(img) return imgs
def crop_img(oimg, name, isImg): imageParts = resizedImage / croppedImage for z in range(1, int(imageParts + 1)): for x in range(1, int(imageParts + 1)): img = oimg[(x * croppedImage) - croppedImage:x * croppedImage, (z * croppedImage) - croppedImage:z * croppedImage] folder = '' if (isImg): folder = cacheImagePath else: folder = cacheMaskPath im = array_to_img(img / 255.0) im.save(folder + name + "_" + str(x) + "-" + str(z) + ".png")
def main(): model = get_model() style_features_group, content_features = get_feature_representations(model) combined_array = tf.contrib.eager.Variable(np.expand_dims(content_array, axis=0), dtype=tf.float32) for i in range(iteration_size): gradients = get_gradient(model, combined_array, style_features_group, content_features) optimizer.apply_gradients([(gradients, combined_array)]) if i % 50 == 0: image.save_img(x=image.array_to_img(deprocess_image(combined_array.numpy()[0])), path='./combined/combine%d.jpg' % i)
def save_imgs(path, imgs, rows, cols): """画像をタイル状にならべて保存する Arguments: path (str): 保存先のファイルパス imgs (np.array): 保存する画像のリスト rows (int): タイルの縦のサイズ cols (int): タイルの横のサイズ Returns: None """ base_width = imgs.shape[1] base_height = imgs.shape[2] channels = imgs.shape[3] output_shape = (base_height * rows, base_width * cols, channels) buffer = np.zeros(output_shape) for row in range(rows): for col in range(cols): img = imgs[row * cols + col] buffer[row * base_height:(row + 1) * base_height, col * base_width:(col + 1) * base_width] = img array_to_img(buffer).save(path)
def plot_gradcam(heatmap, image, grad: bool = True, name: str = None, dim: int = 1024, alpha=0.4) -> str: """ Plota o gradCam probabilstico recebendo como parametro o mapa de calor e a imagem original. Ambos de mesmo tamanho. Args: ----- heatmap (np.array): Mapa de calor image (np.array): Imagem original """ heatmap = np.uint8(255 * heatmap) jet = cm.get_cmap("jet") jet_color = jet(np.arange(256))[:, :3] jet_heatmap = jet_color[heatmap] jet_heatmap = array_to_img(jet_heatmap) jet_heatmap = jet_heatmap.resize((dim, dim)) jet_heatmap = img_to_array(jet_heatmap) superimposed_image = jet_heatmap * alpha + image superimposed_image = array_to_img(superimposed_image) fig = plt.figure() plt.imshow(superimposed_image) plt.show() # Salvar imagem path = '' if name is not None: path = '{}.png'.format(name) plt.savefig(path, dpi=fig.dpi) if grad: plt.show() return path
def _get_batches_of_transformed_samples(self, index_array): batch_x = np.zeros((len(index_array), ) + self.image_shape, dtype=floatx()) grayscale = self.color_mode == 'grayscale' # Build batch of image data for i, j in enumerate(index_array): fname = self.filenames[j] img = load_img(os.path.join(self.directory, fname), grayscale=grayscale, target_size=None, interpolation=self.interpolation) x = img_to_array(img, data_format=self.data_format) # Pillow images should be closed after `load_img`, but not PIL images. if hasattr(img, 'close'): img.close() x = self.image_data_generator.standardize(x) batch_x[i] = x # Optionally save augmented images to disk for debugging purposes if self.save_to_dir: for i, j in enumerate(index_array): img = array_to_img(batch_x[i], self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e7), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) # Build batch of labels if self.class_mode == 'input': batch_y = batch_x.copy() elif self.class_mode == 'sparse': batch_y = self.classes[index_array] elif self.class_mode == 'binary': batch_y = self.classes[index_array].astype(floatx()) elif self.class_mode == 'categorical': batch_y = np.zeros((len(batch_x), self.num_classes), dtype=floatx()) for i, label in enumerate(self.classes[index_array]): batch_y[i, label] = 1. else: return batch_x return batch_x, batch_y
def train(latent_dim, height, width, channels, num_class): (X_train, Y_train), (_, _) = keras.datasets.mnist.load_data() X_train = X_train[0:500] Y_train = Y_train[0:500] Y_train = to_categorical(Y_train, num_class) # convert data into one-hot vectors # X_train = X_train.reshape((X_train.shape[0], ) + (height, width, channels)).astype('float32') X_train = X_train.astype('float32') X_train = X_train[:, :, :, None] X_train = normalize(X_train) # epochs = 50 # batch_size = 128 epochs = 3 batch_size = 128 iterations = int(X_train.shape[0] // batch_size) dcgan = ConditionalDCGAN(latent_dim, height, width, channels, num_class) for epoch in range(epochs): for iteration in range(iterations): real_images = X_train[iteration * batch_size:(iteration + 1) * batch_size] conditions = Y_train[iteration * batch_size:(iteration + 1) * batch_size] d_loss, g_loss = dcgan.train(real_images, conditions, batch_size) # show the progress of learning if (iteration + 1) % 2 == 0: print('{} / {}'.format(iteration + 1, iterations)) print('discriminator loss: {}'.format(d_loss)) print('generator loss: {}'.format(g_loss)) print() with open('loss.txt', 'a') as f: f.write(str(d_loss) + ',' + str(g_loss) + '\r') if (epoch + 1) % 2 == 0: dcgan.save_weights('gan' + '_epoch' + str(epoch + 1) + '.h5') random_latent_vectors = np.random.normal(size=(batch_size, latent_dim)) generated_images = dcgan.predict(random_latent_vectors, conditions) # save generated images for i, generated_image in enumerate(generated_images): img = denormalize(generated_image) img = image.array_to_img(img, scale=False) condition = np.argmax(conditions[1]) img.save( os.path.join('generated_images', str(epoch) + '_' + str(condition) + '.png')) print('epoch' + str(epoch) + ' end') print()
def plot_augment_image(train_dir): train_cats_dir = os.path.join(train_dir, 'cats') fnames = [os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir)] img_path = fnames[random.randint(0, len(fnames)-1)] img = image.load_img(img_path, target_size=(150, 150)) x = image.img_to_array(img) x = x.reshape((1,) + x.shape) i = 0 for batch in datagen.flow(x, batch_size=1): plt.figure(i) imgplot = plt.imshow(image.array_to_img(batch[0])) i += 1 if i % 4 == 0: break plt.show()
def predict(latent_dim, height, width, channels, num_class): dcgan = ConditionalDCGAN(latent_dim, height, width, channels, num_class) # dcgan.load_weights('gan_epoch50.h5') # load weights after 50 times learning dcgan.load_weights('gan_epoch2.h5') # load weights after 1 times learning for num in range(num_class): for id in range(10): random_latent_vectors = np.random.normal(size=(1, latent_dim)) # create conditions like [0, 0, 1, 0, 0, 0, 0, 0, 0, 0] = 2 condition = np.zeros((1, num_class), dtype=np.float32) condition[0, num] = 1 generated_images = dcgan.predict(random_latent_vectors, condition) img = image.array_to_img(denormalize(generated_images[0]), scale=False) img.save( os.path.join('generated_images', str(num) + '_' + str(id) + '.png'))
def sub_main(): tf.enable_eager_execution() model = get_model() style_features_group, content_features = get_feature_representations( model) combined_array = tf.Variable(np.expand_dims(content_array, axis=0), dtype=tf.float32) for i in range(iteration_size): if i % 50 == 0: print(i) image.save_img(x=image.array_to_img( deprocess_image(combined_array.numpy()[0])), path='./combined/combine%d.jpg' % i) gradient_decent(model, combined_array, style_features_group, content_features)
def save_tsne_grid(img_collection, X_2d, out_res, out_dim): grid = np.dstack( np.meshgrid(np.linspace(0, 1, out_dim), np.linspace(0, 1, out_dim))).reshape(-1, 2) cost_matrix = cdist(grid, X_2d, "sqeuclidean").astype(np.float32) cost_matrix = cost_matrix * (100000 / cost_matrix.max()) row_asses, col_asses, _ = lapjv(cost_matrix) grid_jv = grid[col_asses] out = np.ones((out_dim * out_res, out_dim * out_res, 3)) for pos, img in zip(grid_jv, img_collection[0:to_plot]): h_range = int(np.floor(pos[0] * (out_dim - 1) * out_res)) w_range = int(np.floor(pos[1] * (out_dim - 1) * out_res)) out[h_range:h_range + out_res, w_range:w_range + out_res] = image.img_to_array(img) im = image.array_to_img(out) im.save(out_dir + out_name, quality=100)
def gen_image(model_name, noise, combined=True): dirname = os.path.dirname(__file__) #モデル読み込み model = load_model(os.path.join(dirname, 'models', model_name)) #モデルから画像を生成 imgs = model.predict(noise) images = [] num = noise.shape[0] for i in range(num): images.append(array_to_img(imgs[i])) save_folder = os.path.join(dirname, model_name[:-3] + '_' + get_time_txt()) #保存フォルダ生成 os.makedirs(save_folder, exist_ok=True) for i, img in enumerate(images): #配列を画像データに変換 name = model_name[:-3] + '_' + str(i) + '.png' #画像を保存 img.save(os.path.join(save_folder, name)) print(name + ' was saved.') #すべての画像を1枚にまとめて保存 if combined: from PIL import Image size = (10, (num // 10) + (num % 10 != 0)) isize = images[0].size outlinesize = (16, 16) outlinecolor = (0, 0, 0) width = isize[0] * size[0] + outlinesize[0] * (size[0] + 1) height = isize[1] * size[1] + outlinesize[1] * (size[1] + 1) bg = Image.new('RGB', (width, height), outlinecolor) im_count = 0 for h in range(size[1]): pos_h = h * (isize[1] + outlinesize[1]) + outlinesize[1] for w in range(size[0]): pos_w = w * (isize[0] + outlinesize[0]) + outlinesize[0] bg.paste(images[im_count], (pos_w, pos_h)) im_count += 1 name = model_name[:-3] + '_combined.png' bg.save(os.path.join(save_folder, name)) np.savetxt(os.path.join(save_folder, 'noise'), noise, delimiter=',')