def plot_weights(weights): w_c = weights.sum(axis=1) # 50x1 weights = weights / w_c.reshape((weights.shape[0], 1)) IMG = tile_raster_images( X=weights, img_shape=(28, 28), tile_shape=(10, 5), tile_spacing=(1, 1)) show_image(IMG)
X_train, X_test = load_cars() model = build_model() if not False: model.summary() model.fit({ 'input': X_train, 'autoencoder_feedback': X_train }, nb_epoch=100, batch_size=64, validation_split=0.2, callbacks=[EarlyStopping(patience=12)]) model.save_weights('./cars.neuro', overwrite=True) else: model.load_weights('./cars.neuro') l = model.predict({'input': X_test[:25, ...]}) representations = np.clip(l['autoencoder_feedback'], 0, 1) _r = tile_raster_images(X=keras2rgb(representations), img_shape=(32, 32, 3), tile_shape=(5, 5), tile_spacing=(1, 1)) _o = tile_raster_images(X=keras2rgb(X_test), img_shape=(32, 32, 3), tile_shape=(5, 5), tile_spacing=(1, 1)) show_image([(_o, 'Source'), (_r, 'Representations')])
# -*- coding: utf-8 -*- """ exposure.py Basic functions in scikit-image and matplotlib """ from skimage.io import imread from skimage import exposure import numpy as np import matplotlib.pyplot as plt from helpers import show_image, save_image # Load Lena lena = imread("../lena.jpg") # Red channel red_lena = lena[:,:,0] # Plot histogram and choose fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(13.5,6)) ax0.hist(red_lena.ravel(), bins=100) ax1.imshow(red_lena, cmap="gray", clim=(60.0, 240.0)) plt.show() # Automatic exposure equalization equal_lena = exposure.equalize_hist(red_lena, nbins=256) show_image(equal_lena, colormap="gray")
#model.compile('rmsprop', loss='mean_squared_error') return model if __name__ == '__main__': X_train, X_test = load_cars(python_version=sys.version_info.major) model = build_model_functional() if not False: model.summary() model.fit(X_train, X_train, nb_epoch=100, batch_size=64, validation_split=0.2, callbacks=[EarlyStopping(patience=12)]) model.save_weights('./cars.neuro', overwrite=True) else: model.load_weights('./cars.neuro') l = model.predict(X_test[:25, ...]) representations = np.clip(l, 0, 1) _r = tile_raster_images( X=keras2rgb(representations), img_shape=(32, 32, 3), tile_shape=(5, 5), tile_spacing=(1, 1)) _o = tile_raster_images( X=keras2rgb(X_test), img_shape=(32, 32, 3), tile_shape=(5, 5), tile_spacing=(1, 1)) show_image([(_o, 'Source'), (_r, 'Representations')])
import torch import torchvision from Classifier import Classifier from classes import classes from helpers import show_image, compute_accuracy, get_test_set_and_loader if __name__ == '__main__': torch.multiprocessing.freeze_support() _, test_loader = get_test_set_and_loader() dataiter = iter(test_loader) images, labels = dataiter.next() show_image(torchvision.utils.make_grid(images)) print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4))) classifier = Classifier() classifier.load_state_dict(torch.load('./model_storage/cifar_net.pth')) outputs = classifier(images) _, predicted = torch.max(outputs, 1) print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4))) compute_accuracy(test_loader, classifier)
""" filters.py Basic functions in scikit-image and matplotlib """ from skimage.io import imread from skimage.filters import threshold_otsu from skimage import filters import numpy as np from helpers import show_image, save_image # Load Lena lena = imread("../lena.jpg") # Save the red channel red_lena = lena[:, :, 0] lena_1 = np.copy(red_lena) # set values < mean intensity to 0 mean_intensity = np.mean(red_lena) lena_1[red_lena < mean_intensity] = 250 show_image(lena_1, colormap="gray") # Thresholding with Otsu's method thresh = threshold_otsu(red_lena) lena_2 = red_lena > thresh show_image(lena_2, colormap="gray")
ratio = image.shape[0] / float(resized.shape[0]) b, g, r = cv2.split(resized) # show_image("blue", b) # show_image("green", g) # show_image("red", r) avg_red = np.average(r) avg_green = np.average(g) avg_blue = np.average(b) thresh_red = cv2.threshold(r, avg_red + 10, 255, cv2.THRESH_BINARY)[1] thresh_green = cv2.threshold(g, avg_green + 10, 255, cv2.THRESH_BINARY)[1] thresh_blue = cv2.threshold(b, avg_blue + 10, 255, cv2.THRESH_BINARY)[1] show_image("blue", thresh_blue) show_image("green", thresh_green) show_image("red", thresh_red) for item in [thresh_green, thresh_blue, thresh_red]: contours = cv2.findContours(item.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = contours[0] if imutils.is_cv2() else contours[1] sd = ShapeDetector() # loop over the contours for contour in contours: print("contour") # compute the center of the contour, then detect the name of the # shape using only the contour
from skimage.morphology import disk from skimage.filters.rank import mean from skimage.color import rgb2gray from scipy import fftpack from helpers import show_image, save_image # Load Lena and Lena_with_glasses lena = imread("../lena.jpg") lena_glass = imread("../lena_glass.jpg") # ====== Point operators ======= # ------ Pixelweise subtraction ------ glass = lena - lena_glass show_image(glass) # ====== Local operators ======= # ------ Mean filter ------ mean_lena = mean(rgb2gray(lena), disk(5)) show_image(mean_lena, colormap="gray") # ====== Global operators ====== # ------ Fourier transformation ------ F1 = fftpack.fft2(rgb2gray(lena)) F2 = fftpack.fftshift(F1) F2_copy = np.copy(F2) # Calculate a 2D power spectrum psd2D = np.abs(F2)
# gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) # blurred = cv2.GaussianBlur(resized, (3, 3), 0) cannied = imutils.auto_canny(v) dilation = cv2.dilate(cannied, cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)), iterations=5) closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))) erosion = cv2.erode(closing, cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)), iterations=3) # show_image("Input image", display_image) # show_image("Grayscale image", gray) # show_image("Blurred image", blurred) show_image("Cannied image", cannied) show_image("Closed image", erosion) # thresh = cv2.threshold(blurred, avg_gray_color, 255, cv2.THRESH_BINARY)[1] # find contours in the thresholded image and initialize the shape detector contours = cv2.findContours(erosion.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = contours[0] if imutils.is_cv2() else contours[1] # Find biggest contour biggest = None biggest_size = 0 for contour in contours: rect = cv2.minAreaRect(contour)
""" filters.py Basic functions in scikit-image and matplotlib """ from skimage.io import imread from skimage.filters import threshold_otsu from skimage import filters import numpy as np from helpers import show_image, save_image # Load Lena lena = imread("../lena.jpg") # Save the red channel red_lena = lena[:,:,0] lena_1 = np.copy(red_lena) # set values < mean intensity to 0 mean_intensity = np.mean(red_lena) lena_1[red_lena<mean_intensity] = 250 show_image(lena_1, colormap="gray") # Thresholding with Otsu's method thresh = threshold_otsu(red_lena) lena_2 = red_lena > thresh show_image(lena_2, colormap="gray")
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Wed Apr 18 14:03:21 2018 @author: wisse """ from helpers import show_image import numpy as np data = np.loadtxt('speedUp.csv', delimiter=',', dtype=object) for obs in data[0:5]: filename = obs[0] show_image(filename)
from skimage.io import imread from skimage.color import convert_colorspace, rgb2gray from helpers import show_image, save_image # Load image lena = imread("../lena.jpg") # Draw and display an image plt.imshow(lena) plt.show() # Matrix indices are [rows, columns] # Extract the rows 100 to 200 & all columns sub_lena = lena[100:200, 0:-1] show_image(sub_lena) # Accessing color channels: third index -> [rows, cols, channel] red_lena = lena[:, :, 0] show_image(red_lena, cmap="gray") # Convert to grayscale and use different colormap gray_lena = rgb2gray(lena) show_image(gray_lena, colormap="inferno") # Convert to other colorspace hsv_lena = convert_colorspace(lena, "RGB", "HSV") # Only show saturation sat_lena = hsv_lena[:, :, 1] show_image(sat_lena, "gray")
from skimage.io import imread from skimage.color import convert_colorspace, rgb2gray from helpers import show_image, save_image # Load image lena = imread("../lena.jpg") # Draw and display an image plt.imshow(lena) plt.show() # Matrix indices are [rows, columns] # Extract the rows 100 to 200 & all columns sub_lena = lena[100:200 , 0:-1] show_image(sub_lena) # Accessing color channels: third index -> [rows, cols, channel] red_lena = lena[:,:,0] show_image(red_lena, cmap="gray") # Convert to grayscale and use different colormap gray_lena = rgb2gray(lena) show_image(gray_lena, colormap="inferno") # Convert to other colorspace hsv_lena = convert_colorspace(lena, "RGB", "HSV") # Only show saturation sat_lena = hsv_lena[:,:,1] show_image(sat_lena, "gray")