def predict_images(net, images, n_frames=10, checkpoint='final_pixelate.ckpt', size=300): with tf.Session(graph=net['graph']) as sess: saver = tf.train.Saver() saver.restore(sess, 'final_pixelate.ckpt') combined = [] for img_i, img in enumerate(images): actual = img.copy() processed = np.reshape(process(preprocess_img(img)), (100, 100)) img = utils.imcrop_tosquare(actual) img = resize(img, (size, size)) img = np.dot(img[..., :3], [0.299, 0.587, 0.114]) / 255.0 Xs = [] for frame_i in range(n_frames): combined.append(img) for frame_i in range(n_frames): feed_dict = {} feed_dict[net['X']] = processed preds = sess.run([net['Y_pred']], feed_dict=feed_dict) guessed_img = resize(deprocess(preds), (size, size)) combined.append(guessed_img) return combined
def get_celeb_images(): print('##Start get_celeb_images##') dirname = "C:\\Users\\rahmanim\\Documents\\1TF-Gits\\ImagesFromTheInternet" filenames = [os.path.join(dirname, fname) for fname in os.listdir(dirname)] # Make sure we have exactly 100 image files! filenames = filenames[:100] assert (len(filenames) == 100) #print(filenames) # Read every filename as an RGB image imgs = [plt.imread(fname)[..., :3] for fname in filenames] # Crop every image to a square imgs = [utils.imcrop_tosquare(img_i) for img_i in imgs] # Then resize the square image to 100 x 100 pixels; mode='reflect' imgs = [resize(img_i, (100, 100), mode='reflect') for img_i in imgs] # Finally make our list of 3-D images a 4-D array with the first dimension the number of images: imgs = np.array(imgs).astype(np.float32) # Plot the resulting dataset: # Make sure you "run" this cell after you create your `imgs` variable as a 4-D array! # Make sure we have a 100 x 100 x 100 x 3 dimension array assert (imgs.shape == (100, 100, 100, 3)) plt.figure(figsize=(10, 10)) plt.imshow(utils.montage(imgs, saveto='dataset.png')) plt.title('Dataset of 100 images') #plt.show() return imgs
def preprocess_img(img): with warnings.catch_warnings(): img = utils.imcrop_tosquare(img) img = resize(img, (100, 100)) img = img_as_ubyte(img) full = np.full([100, 100, 3], 255, dtype='int64') img = full - img img = np.dot(img[..., :3], [0.299, 0.587, 0.114]) img /= 255.0 channels = 10 new_img = [] for row_i, row in enumerate(img): new_img.append([]) for idx, pixel in enumerate(row): new_img[row_i].append(1 - (round(pixel * channels) / channels)) return new_img
def test_crop(): assert (utils.imcrop_tosquare(np.zeros((64, 23))).shape == (23, 23)) assert (utils.imcrop_tosquare(np.zeros((23, 53))).shape == (23, 23)) assert (utils.imcrop_tosquare(np.zeros((23, 23))).shape == (23, 23)) assert (utils.imcrop_tosquare(np.zeros((24, 53))).shape == (24, 24))
filenames = image_filenames # Read every filename as an RGB image filenames = [ os.path.join(foldername, fname) for fname in os.listdir(foldername) if not os.path.isdir(fname) and 'train' in fname ] imgs = [plt.imread(fname)[..., :3] for fname in filenames] # Check images: need to have all the same number of channels # in shape (W, H, C) C must be the same for all images [print(img_index, ', ', img.shape) for img_index, img in enumerate(imgs)] from libs import utils # Crop every image to a square imgs = [utils.imcrop_tosquare(img_i) for img_i in imgs] # Then resize the square image to 100 x 100 pixels from skimage.transform import resize imgs = [resize(img_i, (100, 100)) for img_i in imgs] # Finally make our list of 3-D images a 4-D array with the first dimension the number of images: import numpy as np imgs = np.array(imgs).astype(np.float32) # Plot the resulting dataset: # Make sure you "run" this cell after you create your `imgs` variable as a 4-D array! # Make sure we have a 100 x 100 x 100 x 3 dimension array assert (imgs.shape == (100, 100, 100, 3)) plt.figure(figsize=(10, 10)) plt.imshow(utils.montage(imgs, saveto='dataset.png'))
get_ipython().magic('ls') plt.style.use('ggplot') from libs import utils utils.remove_duplicates('../daisy/') #example of reload import importlib importlib.reload(utils) import os files = [f for f in os.listdir('../daisy') if '.jpg' in f] len(files) images = [plt.imread('../daisy/' + f) for f in files] images_sq = [utils.imcrop_tosquare(i) for i in images] min_img_sq = min(images_sq, key = lambda im: im.shape[1]) min_img_sq.shape plt.imshow(min_img_sq[:,:,0], cmap='gray') plt.imshow(min_img_sq[:,:,1], cmap='gray') plt.imshow(min_img_sq[:,:,2], cmap='gray') from scipy.misc import imresize images_min = [imresize(im, (177, 177)) for im in images_sq] data_min = np.array(images_min) data_min.shape mean_daisy = np.mean(data_min, axis=0).astype(np.uint8)
def test_crop(): assert(utils.imcrop_tosquare(np.zeros((64, 23))).shape == (23, 23)) assert(utils.imcrop_tosquare(np.zeros((23, 53))).shape == (23, 23)) assert(utils.imcrop_tosquare(np.zeros((23, 23))).shape == (23, 23)) assert(utils.imcrop_tosquare(np.zeros((24, 53))).shape == (24, 24))