def score_grid(): """ Classify with the gridded SP. """ p = 'results\\mnist_filter' (tr_x, tr_y), (te_x, te_y) = load_mnist() # Get the SPs sps = [load(os.path.join(p, sp)) for sp in os.listdir(p) if sp[2] == '0'] sp2 = load(os.path.join(p, 'sp1-0.pkl')) nwindows = 26 ** 2 nfeat = 100 * nwindows # w = [sp2.p[sp2.syn_map == j] for j in xrange(nfeat)] # ms = max(wi.shape[0] for wi in w) # with open(os.path.join(p, 'data.pkl'), 'wb') as f: # cPickle.dump((w, ms), f, cPickle.HIGHEST_PROTOCOL) with open(os.path.join(p, 'data.pkl'), 'rb') as f: w, ms = cPickle.load(f) # Get training data tr_x2 = np.zeros((tr_x.shape[0], nfeat)) for i, x in enumerate(tr_x): nx = extract_patches_2d(x.reshape(28, 28), (3, 3)).reshape( nwindows, 9) x = np.array(np.zeros(nfeat), dtype='bool') for j, (xi, sp) in enumerate(izip(nx, sps)): sp.step(xi) x[j*100:(j*100)+100] = sp.y[:, 0] y = sp2.p * x[sp2.syn_map] w = np.zeros((nfeat, ms)) for j in xrange(nfeat): a = y[sp2.syn_map == j] w[j][:a.shape[0]] = a tr_x2[i] = np.mean(w, 1) # Get testing data te_x2 = np.zeros((te_x.shape[0], nfeat)) for i, x in enumerate(te_x): nx = extract_patches_2d(x.reshape(28, 28), (3, 3)).reshape( nwindows, 9) x = np.array(np.zeros(nfeat), dtype='bool') for j, (xi, sp) in enumerate(izip(nx, sps)): sp.step(xi) x[j*100:(j*100)+100] = sp.y[:, 0] y = sp2.p * x[sp2.syn_map] w = np.zeros((nfeat, ms)) for j in xrange(nfeat): a = y[sp2.syn_map == j] w[j][:a.shape[0]] = a te_x2[i] = np.mean(w, 1) # Classify clf = LinearSVC(random_state=123456789) clf.fit(tr_x2, tr_y) print 'SVM Accuracy : {0:2.2f} %'.format(clf.score(te_x2, te_y) * 100)
def predict_image(self, test_img): """ predicts classes of input image :param test_img: filepath to image to predict on :param show: displays segmentation results :return: segmented result """ img = np.array( rgb2gray( imread( test_img ).astype( 'float' ) ).reshape( 5, 216, 160 )[-2] ) / 256 plist = [] # create patches from an entire slice img_1 = adjust_sigmoid( img ).astype( float ) edges_1 = adjust_sigmoid( img, inv=True ).astype( float ) edges_2 = img_1 edges_5_n = normalize( laplace( img_1 ) ) edges_5_n = img_as_float( img_as_ubyte( edges_5_n ) ) plist.append( extract_patches_2d( edges_1, (23, 23) ) ) plist.append( extract_patches_2d( edges_2, (23, 23) ) ) plist.append( extract_patches_2d( edges_5_n, (23, 23) ) ) patches = np.array( zip( np.array( plist[0] ), np.array( plist[1] ), np.array( plist[2] ) ) ) # predict classes of each pixel based on model full_pred = self.model.predict_classes( patches ) fp1 = full_pred.reshape( 194, 138 ) return fp1
def extract_patches(img, thumb_size, img_size, max): img_patches = [] if args.verbose: print "Creating thumbnail with size ", thumb_size img = resize1(img, thumb_size) #img.thumbnail((thumb_size, thumb_size)) print img.size if img.size[0] > img_size or img.size[1] > img_size: img_array = numpy.array(img) try: patches = image.extract_patches_2d(image=img_array, patch_size=(img_size, img_size), max_patches=max, random_state=0) except: patches = image.extract_patches_2d(image=img_array, patch_size=(img_size, img_size), max_patches=1, random_state=0) for patch in patches: img_patches.append(patch) else: img_array = numpy.array(img) img_patches.append(img_array) if args.verbose: print "Patches extracted: ", len(img_patches) return img_patches
def extractTrainingData1(self): # kernel = np.ones((self.kernelNormalization,self.kernelNormalization)) / self.kernelNormalization**2 # print("Normalizing images...") # for i in range(300): # ps.util.progressbar(i, 300) # meanMap = sp.signal.convolve2d(images[:,:,i], kernel, mode='same', boundary='symm') # t1 = sp.signal.convolve2d(images[:,:,i]**2, kernel, mode='same', boundary='symm') # t2 = sp.signal.convolve2d(images[:,:,i] * meanMap, kernel, mode='same', boundary='symm') # t3 = sp.signal.convolve2d(meanMap**2, kernel, mode='same', boundary='symm') # sigmaMap = np.sqrt(t1 + t3 - 2.0*t2) # images[:,:,i] = (images[:,:,i] - meanMap) / (sigmaMap + 1e-8) # hdu = fits.open('data/20160525-images.fits.gz') # images2 = hdu[0].data.T # piece = images2[140-3:140+4,140-3:140+4,i] # mn = np.sum(piece) # std = np.sqrt(np.sum((piece - mn)**2)) # print(mn, std) # print(meanMap[140,140] / mn, sigmaMap[140,140] / std) # stop() for i in range(300): maxVal = np.max(self.images[:,:,i]) minVal = np.min(self.images[:,:,i]) self.images[:,:,i] = 2.0 * self.images[:,:,i] / (maxVal - minVal) + (-maxVal - minVal) / (maxVal - minVal) print("Extracting images...") self.XTrainSet = np.zeros((300,self.nImages,self.patchSize,self.patchSize)) self.YTrainSet = np.zeros((300,self.nImages)) self.XTestSet = np.zeros((300,self.nImages,self.patchSize,self.patchSize)) self.YTestSet = np.zeros((300,self.nImages)) for i in range(300): self.XTrainSet[i,:,:,:] = extract_patches_2d(self.images[:,:,i], patch_size=(self.patchSize,self.patchSize), max_patches=self.nImages, random_state=123) self.YTrainSet[i,:] = self.strehl[i] self.XTestSet[i,:,:,:] = extract_patches_2d(self.images[:,:,i], patch_size=(self.patchSize,self.patchSize), max_patches=self.nImages, random_state=231) self.YTestSet[i,:] = self.strehl[i] # reorder = np.random.permutation(300) # self.XTrainSet = self.XTrainSet[reorder,:] # self.YTrainSet = self.YTrainSet[reorder,:] # reorder = np.random.permutation(300) # self.XTestSet = self.XTestSet[reorder,:] # self.YTestSet = self.YTestSet[reorder,:] self.XTrainSet = self.XTrainSet.reshape((300*self.nImages,1,self.patchSize,self.patchSize)) self.YTrainSet = self.YTrainSet.reshape((300*self.nImages,1)) self.XTestSet = self.XTestSet.reshape((300*self.nImages,1,self.patchSize,self.patchSize)) self.YTestSet = self.YTestSet.reshape((300*self.nImages,1)) min_max_scaler = preprocessing.MinMaxScaler() self.YTrainSet = min_max_scaler.fit_transform(self.YTrainSet)
def predict_image(self, filepath_image, show=False): ''' predicts classes of input image INPUT (1) str 'filepath_image': filepath to image to predict on (2) bool 'show': True to show the results of prediction, False to return prediction OUTPUT (1) if show == False: array of predicted pixel classes for the center 208 x 208 pixels (2) if show == True: displays segmentation results ''' print 'Starting prediction...' if self.cascade_model: images = io.imread(filepath_image).astype('float').reshape(5, 216, 160) p33list = [] p65list = [] # create patches from an entire slice for image in images[:-1]: if np.max(image) != 0: image /= np.max(image) patch65 = extract_patches_2d(image, (65, 65)) p65list.append(patch65) p33list.append(self.center_n(33, patch65)) print str(len(p33list)) patches33 = np.array(zip(p33list[0], p33list[1], p33list[2], p33list[3])) patches65 = np.array(zip(p65list[0], p65list[1], p65list[2], p65list[3])) # predict classes of each pixel based on model prediction = self.model.predict([patches65, patches33]) print 'Predicted' prediction = prediction.reshape(208, 208) if show: io.imshow(prediction) plt.show else: return prediction else: images = io.imread(filepath_image).astype('float').reshape(5, 216, 160) p33list = [] # create patches from an entire slice for image in images[:-1]: if np.max(image) != 0: image /= np.max(image) patch33 = extract_patches_2d(image, (33, 33)) p33list.append(patch33) patches33 = np.array(zip(p33list[0], p33list[1], p33list[2], p33list[3])) # predict classes of each pixel based on model prediction = self.cnn1.predict(patches33) print 'Predicted' prediction = prediction.reshape(5, 184, 128) predicted_classes = np.argmax(prediction, axis=0) if show: print 'Let s show' for i in range(5): io.imshow(prediction[i]) plt.show print 'Showed' return prediction else: return predicted_classes
def patch_and_Gnormalize(img, patch_size, verbose=True): """1. Extract patches from an image 2. Cast to a 2D matrix 3. normalize the patches like normal r.v.s Make sure that dtype(img) is float, so that normalization makes sense! Parameters ---------- img: 3D array of float, shape (M, N, C) Input data, M rows, N columns, C colour channels patch_size: 2D tuple, shape (patch_width, patch_height) Usually (10, 10) verbose: controls verbosity. True by default. Returns ------- patches: 2D array of float, shape (M2, N2) M2 ~= M*N, N2 = patch_width * patch_height * C M2 will likely be less than M * N because of how extract_patches_2d works """ t0 = time.time() if verbose: print('image shape = %s' % (img.shape,)) #1. Extract patches patches = extract_patches_2d(img, patch_size) #2. Cast into 2D patches = patches.reshape(patches.shape[0], -1) #3. GNormalize t1 = time.time() patch_size = (10,10) data = extract_patches_2d(img,patch_size) dt = time.time() - t1 if verbose: print('Extracted patches. Current shape = %s' % (data.shape,)) t1 = time.time() data = data.reshape(data.shape[0],-1) if verbose: print('Reshaped patches. Current shape = %s' % (data.shape,)) data -= np.mean(data, axis = 0) data /= np.std(data, axis = 0) data[np.isnan(data)] = 0.0 dt = time.time() - t0 if verbose: print('Total time elapsed = %.3fs.' % (time.time() - t0)) return patches
def calculate_feature(self): moveConverted = self.convert_board(self.move,0) data = image.extract_patches_2d(moveConverted,self.patch_size) data = data.reshape(data.shape[0], -1) moveConverted = np.rot90(moveConverted) data2 = image.extract_patches_2d(moveConverted,self.patch_size) data2 = data2.reshape(data2.shape[0], -1) data = np.concatenate((data, data2), axis=0) moveConverted = np.rot90(moveConverted) data2 = image.extract_patches_2d(moveConverted,self.patch_size) data2 = data2.reshape(data2.shape[0], -1) data = np.concatenate((data, data2), axis=0) moveConverted = np.rot90(moveConverted) data2 = image.extract_patches_2d(moveConverted,self.patch_size) data2 = data2.reshape(data2.shape[0], -1) data = np.concatenate((data, data2), axis=0) moveConverted = np.rot90(moveConverted) moveConverted = np.fliplr(moveConverted) data2 = image.extract_patches_2d(moveConverted,self.patch_size) data2 = data2.reshape(data2.shape[0], -1) data = np.concatenate((data, data2), axis=0) moveConverted = np.rot90(moveConverted) data2 = image.extract_patches_2d(moveConverted,self.patch_size) data2 = data2.reshape(data2.shape[0], -1) data = np.concatenate((data, data2), axis=0) moveConverted = np.rot90(moveConverted) data2 = image.extract_patches_2d(moveConverted,self.patch_size) data2 = data2.reshape(data2.shape[0], -1) data = np.concatenate((data, data2), axis=0) moveConverted = np.rot90(moveConverted) data2 = image.extract_patches_2d(moveConverted,self.patch_size) data2 = data2.reshape(data2.shape[0], -1) data = np.concatenate((data, data2), axis=0) return data
def test_extract_patches_max_patches(): face = downsampled_face i_h, i_w = face.shape p_h, p_w = 16, 16 patches = extract_patches_2d(face, (p_h, p_w), max_patches=100) assert_equal(patches.shape, (100, p_h, p_w)) expected_n_patches = int(0.5 * (i_h - p_h + 1) * (i_w - p_w + 1)) patches = extract_patches_2d(face, (p_h, p_w), max_patches=0.5) assert_equal(patches.shape, (expected_n_patches, p_h, p_w)) assert_raises(ValueError, extract_patches_2d, face, (p_h, p_w), max_patches=2.0) assert_raises(ValueError, extract_patches_2d, face, (p_h, p_w), max_patches=-1.0)
def ExtractPatches2D(im, win_size): """Function to extract the 2D patches which which will feed haralick Parameters ---------- im: ndarray 2D array containing the image information win_size: tuple Array containing 2 values defining the window size in order to perform the extraction Returns ------- patches: array, shape = (n_patches, patch_height, patch_width) The collection of patches extracted from the image. """ if len(im.shape) != 2: raise ValueError('extraction.Extract2DPatches: The image can only be a' ' 2D image.') if len(win_size) != 2: raise ValueError('extraction.Extract2DPatches: The win_size can only be' ' a tuple with 2 values.') return image.extract_patches_2d(im, win_size)
def test_reconstruct_patches_perfect_color(): face = orange_face p_h, p_w = 16, 16 patches = extract_patches_2d(face, (p_h, p_w)) face_reconstructed = reconstruct_from_patches_2d(patches, face.shape) np.testing.assert_array_almost_equal(face, face_reconstructed)
def test_extract_patches_all(): face = downsampled_face i_h, i_w = face.shape p_h, p_w = 16, 16 expected_n_patches = (i_h - p_h + 1) * (i_w - p_w + 1) patches = extract_patches_2d(face, (p_h, p_w)) assert_equal(patches.shape, (expected_n_patches, p_h, p_w))
def test_extract_patches_all_color(): face = orange_face i_h, i_w = face.shape[:2] p_h, p_w = 16, 16 expected_n_patches = (i_h - p_h + 1) * (i_w - p_w + 1) patches = extract_patches_2d(face, (p_h, p_w)) assert_equal(patches.shape, (expected_n_patches, p_h, p_w, 3))
def fit(self, X, y=None): # compute the codes print 'Extracting patchs...' patchs = [] num = self.patch_num // X.size for x in X: img = imread(str(x[0])) tmp = extract_patches_2d(img, (self.patch_size,self.patch_size), \ max_patches=num, random_state=np.random.RandomState()) patchs.append(tmp) data = np.vstack(patchs) data = data.reshape(data.shape[0], -1) data -= np.mean(data, axis=0) data = data/np.std(data, axis=0) print 'Learning codebook...' if self.method == 'sc': self.dico = MiniBatchDictionaryLearning(n_components=self.codebook_size, \ alpha=1, n_iter=100, batch_size =100, verbose=True) self.dico.fit(data) elif self.method=='km': # self.dico = MiniBatchKMeans(n_clusters=self.codebook_size) pass return self
def get_data(self, data, max_patches=None): """ Extracts raw data from patches. """ max_patches = max_patches or self.num_patches if isinstance(data, np.ndarray): # one image patches = extract_patches_2d( data, (self.basement_size, self.basement_size), max_patches=max_patches) else: patches = [] # multiple images for i in xrange(max_patches): idx = np.random.randint(len(data)) # selecting random image dx = dy = self.basement_size if data[idx].shape[0] <= dx or data[idx].shape[1] <= dy: continue x = np.random.randint(data[idx].shape[0] - dx) y = np.random.randint(data[idx].shape[1] - dy) patch = data[idx][x: x + dx, y: y + dy] patches.append(patch.reshape(-1)) patches = np.vstack(patches) patches = patches.reshape(patches.shape[0], self.basement_size, self.basement_size) print 'patches', patches.shape patches = preprocessing.scale(patches) return patches
def fit(self, X, y=None): num = self.patch_num // X.size data = [] for item in X: img = imread(str(item[0])) img = img_as_ubyte(rgb2gray(img)) #img = self.binary(img) # 二值化 tmp = extract_patches_2d(img, self.patch_size, max_patches = num,\ random_state=np.random.RandomState()) data.append(tmp) data = np.vstack(data) data = data.reshape(data.shape[0], -1) data = np.asarray(data, 'float32') # 二值化后不需要0-1归化 data = data - np.min(data, 0) data = data/(np.max(data, 0) + 0.0001) # 0-1 scaling self.rbm = BernoulliRBM(n_components=self.n_components,\ learning_rate=self.learning_rate, \ n_iter=self.n_iter,\ batch_size=self.batch_size,\ verbose=True) self.rbm.fit(data) return self
def transform(self, X): """ Parameters ---------- X : {array-like}, shape = [n_samples, 1] Training vectors, where n_samples is the number of samples and 1 is image path. Returns ------- array-like = [n_samples, features] Class labels predicted by each classifier. """ print 'Extracting feature...' # setting the dictionary self.dico.set_params(transform_algorithm='lars') results = [] for sample in X: img = imread(str(sample[0])) tmp = extract_patches_2d(img, (self.patch_size,self.patch_size), \ max_patches=300, random_state=np.random.RandomState()) data = tmp.reshape(tmp.shape[0], -1) data = data-np.mean(data, axis=0) data = data/np.std(data, axis=0) code = self.dico.transform(data) results.append(code.sum(axis=0)) return np.vstack(results)
def get_data(self, name_database='serre07_distractors', seed=None): if self.verbose: # setup toolbar sys.stdout.write('Extracting data...') sys.stdout.flush() sys.stdout.write("\b" * (toolbar_width+1)) # return to start of line, after '[' t0 = time.time() imagelist = self.slip.make_imagelist(name_database=name_database)#, seed=seed) for filename, croparea in imagelist: # whitening image, filename_, croparea_ = self.slip.patch(name_database, filename=filename, croparea=croparea, center=False)#, , seed=seed) image = self.slip.whitening(image) # Extract all reference patches data_ = extract_patches_2d(image, self.patch_size, max_patches=int(self.max_patches))#, seed=seed) data_ = data_.reshape(data_.shape[0], -1) data_ -= np.mean(data_, axis=0) data_ /= np.std(data_, axis=0) try: data = np.vstack((data, data_)) except: data = data_.copy() if self.verbose: # update the bar sys.stdout.write(filename + ", ") sys.stdout.flush() if self.verbose: dt = time.time() - t0 sys.stdout.write("\n") sys.stdout.write("Data is of shape : "+ str(data.shape)) sys.stdout.write('done in %.2fs.' % dt) sys.stdout.flush() return data
def test_reconstruct_patches_perfect_color(): lena = orange_lena p_h, p_w = 16, 16 patches = extract_patches_2d(lena, (p_h, p_w)) lena_reconstructed = reconstruct_from_patches_2d(patches, lena.shape) np.testing.assert_array_equal(lena, lena_reconstructed)
def predict_image(self, test_img, show=False): ''' predicts classes of input image INPUT (1) str 'test_image': filepath to image to predict on (2) bool 'show': True to show the results of prediction, False to return prediction OUTPUT (1) if show == False: array of predicted pixel classes for the center 208 x 208 pixels (2) if show == True: displays segmentation results ''' imgs = io.imread(test_img).astype('float').reshape(5,240,240) plist = [] # create patches from an entire slice for img in imgs[:-1]: if np.max(img) != 0: img /= np.max(img) p = extract_patches_2d(img, (33,33)) plist.append(p) patches = np.array(zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2]), np.array(plist[3]))) # predict classes of each pixel based on model full_pred = self.model_comp.predict_classes(patches) fp1 = full_pred.reshape(208,208) if show: io.imshow(fp1) plt.show else: return fp1
def fit(self, images): buffer = [] index = 1 t0 = time.time() # The online learning part: cycle over the whole dataset 4 times index = 0 passes = 10 for _ in range(passes): for img in images: data = extract_patches_2d(img, self.patch_size, max_patches=15, random_state=self.rng) data = np.reshape(data, (len(data), -1)) #This casting is only needed for RGB data #buffer.append(data.astype(float)) buffer.append(data) index += 1 #if index % 1000 == 0: if index % (self.nclusters * 2) == 0: data = np.concatenate(buffer, axis=0) data = gcn(data) data = whiten(data) self.kmeans.partial_fit(data) buffer = [] dt = time.time() - t0 print('done in %.2fs.' % dt)
def test_reconstruct_patches_perfect(): face = downsampled_face p_h, p_w = 16, 16 patches = extract_patches_2d(face, (p_h, p_w)) face_reconstructed = reconstruct_from_patches_2d(patches, face.shape) np.testing.assert_array_equal(face, face_reconstructed)
def image_to_components_space(image, encoder, patches_size=(DEFAULT_PATCHE_SIZE,DEFAULT_PATCHE_SIZE)): patches = extract_patches_2d(image, patches_size) patches = numpy.reshape(patches, (patches.shape[0],-1)) encoded = [] for s in gen_even_slices(len(patches), 100): encoded.extend(encoder.transform(patches[s])) return numpy.asarray(encoded)
def predict_image(self, test_img): """ predicts classes of input image :param test_img: filepath to image to predict on :return: segmented result """ # imgs = io.imread(test_img).astype('float').reshape(5, 216, 160) imgs = mpimg.imread(test_img).astype('float') imgs = rgb2gray(imgs).reshape(5, 216, 160) plist = [] # create patches_to_predict from an entire slice for img in imgs[:-1]: if np.max(img) != 0: img /= np.max(img) p = extract_patches_2d(img, (33, 33)) plist.append(p) patches_to_predict = np.array( zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2]), np.array(plist[3]))) # predict classes of each pixel based on model full_pred = self.model.predict_classes(patches_to_predict) fp1 = full_pred.reshape(184, 128) return fp1
def offset_histogram(im, missing_region, patch_size): (im_height, im_width) = im.shape all_patches = image.extract_patches_2d(im, (patch_size, patch_size)) nb_rows = im_height - patch_size + 1 nb_cols = im_width - patch_size + 1 # The array of patches is reshaped to have each patch indexed by its # coordinates in the image all_patches = all_patches.reshape( nb_rows, nb_cols, patch_size, patch_size) #The size of the histogram is determined by the maximum offsets possible hist_height = 2*min(im_height, SEARCH_SPACE_FACTOR*missing_region.getHeight()) hist_width = 2*min(im_width, SEARCH_SPACE_FACTOR*missing_region.getWidth()) hist = np.zeros((hist_height, hist_width)) for i in range(nb_rows): for j in range(nb_cols): (x, y) = closest_patch(i, j, all_patches, missing_region) # The zero of the offsets is the center of the histogram hist[hist_height//2 + x, hist_width//2 + y] += 1 return hist
def test_extract_patches_all_color(): lena = orange_lena i_h, i_w = lena.shape[:2] p_h, p_w = 16, 16 expected_n_patches = (i_h - p_h + 1) * (i_w - p_w + 1) patches = extract_patches_2d(lena, (p_h, p_w)) assert_equal(patches.shape, (expected_n_patches, p_h, p_w, 3))
def test_extract_patches_all(): lena = downsampled_lena i_h, i_w = lena.shape p_h, p_w = 16, 16 expected_n_patches = (i_h - p_h + 1) * (i_w - p_w + 1) patches = extract_patches_2d(lena, (p_h, p_w)) assert_equal(patches.shape, (expected_n_patches, p_h, p_w))
def test_extract_patches_less_than_max_patches(): face = downsampled_face i_h, i_w = face.shape p_h, p_w = 3 * i_h // 4, 3 * i_w // 4 # this is 3185 expected_n_patches = (i_h - p_h + 1) * (i_w - p_w + 1) patches = extract_patches_2d(face, (p_h, p_w), max_patches=4000) assert_equal(patches.shape, (expected_n_patches, p_h, p_w))
def extract_patch(self, img_array, patch_size=(80,96), max_patches=30): """ Reshape a 2D image into a collection of patches and extract it into a dedicated array :return list of patches extracted from the image """ patches = extract_patches_2d(img_array, patch_size, max_patches) self.n_patches = patches.shape[0] return list(patches)
def main(): im = astronaut() patches = extract_patches_2d(im, (96, 96), 10, random_state=0) # save these files on disk file_names = ['sample_{}.png'.format(x) for x in range(10)] for idx, filename in enumerate(file_names): patch_this = patches[idx] if not exists(filename): imsave(filename, patch_this)
def constructPatches(img2, patch_size, scale=True): data = extract_patches_2d(img2, patch_size) data = data.reshape(data.shape[0], -1) if scale: data -= np.mean(data, axis=0) data /= np.std(data, axis=0) return(data)
#predict pass def create_array(self, image): img = Image.open(image) return np.array(img) path = 'data/sample/segments/0/' """ for im in os.listdir(path): img = Image.open(path+im) patches = image.extract_patches_2d(np.array(img), (2, 2), max_patches=2, random_state=0) print patches.shape reconstructed = image.reconstruct_from_patches_2d(patches, (2, 2,2)) img = Image.fromarray(patches, 'L') img.save('my.png') img.show() """ img = Image.open(path + '0.png') im_array = np.array(img) print im_array.shape patches = image.extract_patches_2d(im_array, (5, 5), max_patches=400, random_state=0) print patches.shape print patches reconstructed = image.reconstruct_from_patches_2d(patches, (80, 35)) img = Image.fromarray(reconstructed, 'L') img.save('my.png') img.show()
# Learn the dictionary of images print('Learning the dictionary... ') rng = np.random.RandomState(0) kmeans = MiniBatchKMeans(n_clusters=81, random_state=rng, verbose=True) patch_size = (20, 20) buffer = [] t0 = time.time() # The online learning part: cycle over the whole dataset 6 times index = 0 for _ in range(6): for img in faces.images: data = extract_patches_2d(img, patch_size, max_patches=50, random_state=rng) data = np.reshape(data, (len(data), -1)) buffer.append(data) index += 1 if index % 10 == 0: data = np.concatenate(buffer, axis=0) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) kmeans.partial_fit(data) buffer = [] if index % 100 == 0: print('Partial fit of %4i out of %i' % (index, 6 * len(faces.images))) dt = time.time() - t0
def make_patch(img, label, out_path, period=16, patch=64, npatch=10, scale=1, norm=True, rgb=True, name="fart_town"): ''' Generate new patches for training! Extracts 'npatch' 2d patches per time-slice, for all images. Images are required to be non shuffled to prevent my own confusion ... Each patch corresponds to a randomly extracted portion of the image. Since we are working on a segmentation task, we only save patches where part of the label is present *** note: set to period=16 by default because all my data is period=16 ... ''' assert sum( sum(img[1, :, :, 1] - img[0, :, :, 2])) == 0, """The data loaded is already shuffled, but non-shuffled data is required. \n you can use load_seg_data() to get the data in the right form""" if patch >= 256: npatch = 1 # don't want to reuse the same image each time!... at least for now scans = img.shape[0] // period # determines length of loop samples = npatch * period * scans # determines 0th element of final array psize = float(patch) * scale # determines final patch size psize = (int(psize), int(psize)) # here is the final resting place of our patches: img_patch = np.zeros(shape=(samples, psize[0], psize[1], 3)) label_patch = np.zeros(shape=(samples, psize[0], psize[1], 1)) if patch == 256: img = np.pad(img, [[0, 0], [0, 0], [2, 2], [0, 0]], mode='edge') label = np.pad(label, [[0, 0], [0, 0], [2, 2], [0, 0]], mode='edge') for i in range(scans): # from the img and label arrays, extract portion corresponding to single img/scan/idk start = i * period imgpoo = img[start:start + period, :, :, :] labelpoo = label[start:start + period, :, :, :] if (norm): img_max = imgpoo.max() img_min = imgpoo.min() imgpoo = 100 * (imgpoo - img_min) // (img_max - img_min) # start by extracting npatch # of slices for t in range(period): rs = random.randint(0, 9999) if patch >= 256: # cant get a patc for something bgger than the img hm1 = imgpoo[t, :, :, :] hm2 = labelpoo[t, :, :, :np.newaxis] else: hm1 = extract_patches_2d(imgpoo[t, :, :, :], patch_size=(patch, patch), random_state=rs, max_patches=npatch) hm2 = extract_patches_2d(labelpoo[t, :, :, :], patch_size=(patch, patch), random_state=rs, max_patches=npatch) # array stating whether the patch has labels labelbool = np.zeros(npatch, dtype=bool) for j in range(npatch): #labelbool=np.append(labelbool,1 in hm2[j,:,:]) labelbool[j] = 1 in hm2[j, :, :] # if we weren't able to extract at least 'npatch' patches containing labels, # continue to extract patches until we do while sum(labelbool) < npatch: rs = random.randint(0, 9999) hm12 = extract_patches_2d(imgpoo[1, :, :, :], patch_size=(patch, patch), random_state=rs, max_patches=1) hm22 = extract_patches_2d(labelpoo[1, :, :, :], patch_size=(patch, patch), random_state=rs, max_patches=1) hm1 = np.append(hm1, hm12, axis=0) hm2 = np.append(hm2, hm22, axis=0) labelbool = np.append(labelbool, 1 in hm22) #only keep the patche with labels hm1 = hm1[labelbool, :, :, :] #only keep if true hm2 = hm2[labelbool, :, :, np.newaxis] #allocate to the finale array, scaling the patch if needed if patch >= 256: #omg ... anyways just removed 'k' from everywhere .. otherwise same as below hm1 = imgpoo[t, :, :, :] hm2 = labelpoo[t, :, :, :np.newaxis] if (scale != 1): img_patch[(npatch * period * i) + (npatch * t):, :, :] = rescale(hm1[:, :, :], scale=scale, mode="constant") label_patch[(npatch * period * i) + (npatch * t), :, :, :] = rescale( hm2[:, :, :], scale=scale, mode="constant") else: img_patch[(npatch * period * i) + (npatch * t), :, :, :] = hm1[:, :, :] label_patch[(npatch * period * i) + (npatch * t), :, :, :] = hm2[:, :, :] else: for k in range(npatch): if (scale != 1): img_patch[(npatch * period * i) + (npatch * t) + k, :, :, :] = rescale(hm1[k, :, :, :], scale=scale, mode="constant") label_patch[(npatch * period * i) + (npatch * t) + k, :, :, :] = rescale(hm2[k, :, :, :], scale=scale, mode="constant") else: img_patch[(npatch * period * i) + (npatch * t) + k, :, :, :] = hm1[k, :, :, :] label_patch[(npatch * period * i) + (npatch * t) + k, :, :, :] = hm2[k, :, :, :] np.save(os.path.join(out_path, name + "_img.npy"), img_patch) np.save(os.path.join(out_path, name + "_label.npy"), label_patch) return
def preprocess(self, image): # Extract a random crop from the image with the target width and height. # max_patches=1 -- indicating that we only need a single random patch from the input image. return extract_patches_2d(image, (self.height, self.width), max_patches=1)[0]
def preprocess(self, image): return extract_patches_2d(image, (self.height, self.width), max_patches=1)[0]
def extract_patches(M,S): N,_,_,C = M.shape PATCHES = [] for n in xrange(N): PATCHES.append(stack([extract_patches_2d(M[n,:,:,c],(S,S)) for c in xrange(C)],2)) return asarray(PATCHES).reshape((-1,S*S*C))
import matplotlib.pyplot as plt from PIL import Image import numpy as np from sklearn.feature_extraction import image from sklearn.utils import check_random_state fig, axes = plt.subplots(10, 10) axes = axes.ravel() data = Image.open('DSC_0360.jpg') w, h = data.size data = data.resize((600, 400)) data = np.array(data) patches = image.extract_patches_2d(data, (5, 5)) rng = check_random_state(10) for i, ax in enumerate(axes): i = rng.randint(len(patches)) ax.imshow(patches[i].reshape((5, 5, 3))) ax.axis('off') plt.savefig('patches.png') fig, ax = plt.subplots(1, 1) ax.imshow(data) ax.axis('off') plt.savefig('image.png')
from sklearn.feature_extraction.image import reconstruct_from_patches_2d ############################################################################### # Load Lena image and extract patches lena = lena() / 256.0 # downsample for higher speed lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2] lena /= 4.0 height, width = lena.shape print('Extracting reference patches...') t0 = time() patch_size = (7, 7) data = extract_patches_2d(lena, patch_size) data = data.reshape(data.shape[0], -1) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) print('done in %.2fs.' % (time() - t0)) ############################################################################### # Learn the dictionary from reference patches print('Learning the dictionary...') t0 = time() dico = MiniBatchDictionaryLearning(n_components=100, alpha=1, n_iter=500) V = dico.fit(data).components_ dt = time() - t0 print('done in %.2fs.' % dt)
# since apparently matpltlib doesnt care about float32 vs uint8 img = np.asarray(Image.open(os.path.join(path,f))) imgs.append(img[0:483, 0:645]) l_idx = np.searchsorted(img_name, data['path']) image_set = np.asarray(imgs) imgs_ordered = image_set[l_idx] processed_imgs = [] labels = [] #bar = Bar('Processing', len(imgs_ordered)) print('Generating Images\n') for i in range(len(imgs_ordered)): patches = image.extract_patches_2d(imgs_ordered[i], (224,224), max_patches = 100) label = data['primary_microconstituent'][i] for patch in patches: x = Image.fromarray(patch).convert('RGB') x = np.asarray(x) #x = np.expand_dims(patch, axis = 2) x = preprocess_input(x) processed_imgs.append(x) labels.append(label) progbar(i, (len(imgs_ordered)-1), 20) lb = LabelBinarizer() lb.fit(np.asarray(data['primary_microconstituent'])) y = lb.transform(labels)
def get_dictionary_data(n_comp=20, zero_index=True): unlabeled = util.load_unlabeled_training(flatten=False) height, width = 32, 32 n_images = 10000 patch_size = (8, 8) unlabeled = util.standardize(unlabeled) np.random.shuffle(unlabeled) print('Extracting reference patches...') patches = np.empty((0, 64)) t0 = time() for image in unlabeled[:n_images, :, :]: data = np.array(extract_patches_2d(image, patch_size, max_patches=0.10)) data = data.reshape(data.shape[0], -1) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) + 1e-20 patches = np.concatenate([patches, data]) print('done in %.2fs.' % (time() - t0)) # whiten the patches z = zca.ZCA() z.fit(patches) z.transform(patches) print('Learning the dictionary...') t0 = time() dico = MiniBatchDictionaryLearning(n_components=n_comp, alpha=1) V = dico.fit(patches).components_ dt = time() - t0 print('done in %.2fs.' % dt) #plt.figure(figsize=(4.2, 4)) #for i, comp in enumerate(V[:100]): # plt.subplot(10, 10, i + 1) # plt.imshow(comp.reshape(patch_size), cmap=plt.cm.gray_r, # interpolation='nearest') # plt.xticks(()) # plt.yticks(()) #plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23) #plt.show() labeled_data, labels = util.load_labeled_training(flatten=False, zero_index=True) labeled_data = util.standardize(labeled_data) test_data = util.load_all_test(flatten=False) test_data = util.standardize(test_data) #util.render_matrix(test_data, flattened=False) print('Training SVM with the training images...') t0 = time() reconstructed_images = np.empty((0, 64)) multiplied_labels = np.empty((0)) for i in range(len(labeled_data)): image = labeled_data[i, :, :] label = labels[i] data = extract_patches_2d(image, patch_size, max_patches=0.50) data = data.reshape(data.shape[0], -1) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) + 1e-20 code = dico.transform(data) patches = np.dot(code, V) z.transform(patches) reconstructed_images = np.concatenate([reconstructed_images, patches]) extended_labels = np.asarray([label] * len(patches)) multiplied_labels = np.concatenate([multiplied_labels, extended_labels]) print(reconstructed_images.shape, multiplied_labels.shape) svc = SVC() #print('Getting cross-val scores...') #scores = cross_validation.cross_val_score(svc, reconstructed_images, multiplied_labels, cv=10) #print('cross-val scores:', scores) #print('cross-val mean:', np.mean(scores)) #print('cross-val variance:', np.var(scores)) print('done in %.2fs.' % (time() - t0)) svc.fit(reconstructed_images, multiplied_labels) print('Reconstructing the test images...') t0 = time() predictions = [] for i, image in enumerate(test_data): data = extract_patches_2d(image, patch_size, max_patches=0.25) data = data.reshape(data.shape[0], -1) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) + 1e-20 code = dico.transform(data) patches = np.dot(code, V) z.transform(patches) pred = svc.predict(patches) print('Variance in the predictions:', np.var(pred)) predictions.append(mode(pred)) print('done in %.2fs.' % (time() - t0)) predictions += 1 util.write_results(predictions, 'svm_patches_25_percent_20_comp.csv')
def test_extract_patch_same_size_image(): face = downsampled_face # Request patches of the same size as image # Should return just the single patch a.k.a. the image patches = extract_patches_2d(face, face.shape, max_patches=2) assert_equal(patches.shape[0], 1)
# downsample for higher speed face = face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2] + face[1::2, 1::2] face /= 4.0 height, width = face.shape # Distort the right half of the image print('Distorting image...') distorted = face.copy() distorted[:, width // 2:] += 0.075 * np.random.randn(height, width // 2) # Extract all reference patches from the left half of the image print('Extracting reference patches...') t0 = time() patch_size = (7, 7) data = extract_patches_2d(distorted[:, :width // 2], patch_size) data = data.reshape(data.shape[0], -1) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) print('done in %.2fs.' % (time() - t0)) ############################################################################### # Learn the dictionary from reference patches print('Learning the dictionary...') t0 = time() dico = MiniBatchDictionaryLearning(n_components=100, alpha=1, n_iter=500) V = dico.fit(data).components_ dt = time() - t0 print('done in %.2fs.' % dt)
from keras.optimizers import SGD from keras.optimizers import Adam import numpy as np from PIL import Image import math from sklearn.feature_extraction import image from keras.datasets import mnist import matplotlib.pyplot as plt img = np.asarray( Image.open( '/Users/levimcclenny/Desktop/Materials Informatics/image generation/autoencoders/micrographs/micrograph2.tif' )) img = img[0:483, 0:645] patches = image.extract_patches_2d(img, (64, 64), max_patches=50) patches = np.tile(patches, (1200, 1, 1)) imgs = [] for patch in patches: x = Image.fromarray(patch) x = np.asarray(x) #x = np.expand_dims(patch, axis = 2) imgs.append(x) imgs = np.asarray(imgs) def generator_model(): model = Sequential() model.add(Dense(input_dim=100, output_dim=1024)) model.add(Activation('tanh'))
omp_nnz = None # OMP non-zero coeffs targeted over the n coeffs param_a = 0.5 # parameter for the modified scalar-product # --- Image pre-processing --- # # Read image and get the cmap for plotting img = mpimg.imread(img_file) img = util.img_as_float(img) cmap = 'gray' if len(img.shape) == 2 else None # Add the noise img_with_noise = util.random_noise(img, var=sigma**2) # Extract and concatenate patches to obtain the input patches = image.extract_patches_2d(img, patch_size) Y = patches.reshape(patches.shape[0], -1) # --- Denoising --- # ksvd = KSVD(k=k, maxiter=maxiter, omp_tol=omp_tol, omp_nnz=None, param_a=param_a) ksvd.learn_dictionary(Y) alpha = ksvd.denoise(Y) # --- Image reconstruction and plot --- # img_reconstructed = alpha @ ksvd.dictionary img_reconstructed = image.reconstruct_from_patches_2d( img_reconstructed.reshape(patches.shape), img.shape)
import numpy as np import matplotlib.pyplot as plt from sklearn.feature_extraction import image def patchMatrixGenerator(patches): numPatches = patches.shape[0] output = np.empty([numPatches, 64]) for i in range(numPatches): z = patches[i].flatten('F') output[i, :] = z return output train_cat = np.matrix(np.loadtxt('data/train_cat.txt', delimiter=',')) train_grass = np.matrix(np.loadtxt('data/train_grass.txt', delimiter=',')) Y = plt.imread('data/cat_grass.jpg') / 255 #one_image = load_sample_image('') patches = image.extract_patches_2d(Y, (8, 8)) output = patchMatrixGenerator(patches) np.savetxt('data/cat_grass_pixels.txt', output) #outMatrix = patchMatrixGenerator(Y)
def preprocess(self, image): # extract a random crop from the image with the target width # and height return extract_patches_2d(image, (self.height, self.width), max_patches=1)[0]
def classify_test_data(activate2, W_list, b_list, path, patch_size, prefix, recon_flag, slice_num=1): Flair = [] T1 = [] T2 = [] T_1c = [] Recon = [] Folder = [] Truth = [] Subdir_array = [] label_num = 5 for subdir, dirs, files in os.walk(path): # if len(Flair) is 1: # break for file1 in files: #print file1 if file1[-3:] == 'nii' and ('Flair' in file1): Flair.append(file1) Folder.append(subdir + '/') Subdir_array.append(subdir[-5:]) elif file1[-3:] == 'nii' and ('T1' in file1 and 'T1c' not in file1): T1.append(file1) elif file1[-3:] == 'nii' and ('T2' in file1): T2.append(file1) elif file1[-3:] == 'nii' and ('T1c' in file1 or 'T_1c' in file1): T_1c.append(file1) elif file1[-3:] == 'mha' and 'OT' in file1: Truth.append(file1) elif file1[-3:] == 'mha' and 'Recon' in file1: Recon.append(file1) number_of_images = len(Flair) for image_iterator in range(number_of_images): print 'Iteration : ', image_iterator + 1 print 'Folder : ', Folder[image_iterator] print 'T2: ', T2[image_iterator] print '... predicting' Flair_image = nib.load(Folder[image_iterator] + Flair[image_iterator]) T1_image = nib.load(Folder[image_iterator] + T1[image_iterator]) T2_image = nib.load(Folder[image_iterator] + T2[image_iterator]) T_1c_image = nib.load(Folder[image_iterator] + T_1c[image_iterator]) if recon_flag is True: Recon_image = mha.new(Folder[image_iterator] + Recon[image_iterator]) Flair_image = Flair_image.get_data() T1_image = T1_image.get_data() T2_image = T2_image.get_data() T_1c_image = T_1c_image.get_data() if recon_flag is True: Recon_image = Recon_image.data print 'Input shape: ', Flair_image.shape if slice_num == 2: Flair_image = np.swapaxes(Flair_image, 0, 1) Flair_image = np.swapaxes(Flair_image, 1, 2) T1_image = np.swapaxes(T1_image, 0, 1) T1_image = np.swapaxes(T1_image, 1, 2) T2_image = np.swapaxes(T2_image, 0, 1) T2_image = np.swapaxes(T2_image, 1, 2) T_1c_image = np.swapaxes(T_1c_image, 0, 1) T_1c_image = np.swapaxes(T_1c_image, 1, 2) elif slice_num == 3: Flair_image = np.swapaxes(Flair_image, 0, 1) Flair_image = np.swapaxes(Flair_image, 0, 2) T1_image = np.swapaxes(T1_image, 0, 1) T1_image = np.swapaxes(T1_image, 0, 2) T2_image = np.swapaxes(T2_image, 0, 1) T2_image = np.swapaxes(T2_image, 0, 2) T_1c_image = np.swapaxes(T_1c_image, 0, 1) T_1c_image = np.swapaxes(T_1c_image, 0, 2) xdim, ydim, zdim = Flair_image.shape prediction_image = [] # if slice_num ==1: # prediction_image = np.zeros([(xdim-patch_size+1)*(ydim-patch_size+1), zdim]) # elif slice_num==2 or slice_num==3: # prediction_image = np.zeros([zdim, (ydim-patch_size+1)*(xdim-patch_size+1)]) print print 'shape:', Flair_image.shape for i in range(zdim): Flair_slice = np.transpose(Flair_image[:, :, i]) T1_slice = np.transpose(T1_image[:, :, i]) T2_slice = np.transpose(T2_image[:, :, i]) T_1c_slice = np.transpose(T_1c_image[:, :, i]) if recon_flag is True: Recon_slice = np.transpose(Recon_image[:, :, i]) Flair_patch = image.extract_patches_2d(Flair_slice, (patch_size, patch_size)) F_P = Flair_patch.reshape(len(Flair_patch), patch_size * patch_size) T1_patch = image.extract_patches_2d(T1_slice, (patch_size, patch_size)) T1_P = T1_patch.reshape(len(Flair_patch), patch_size * patch_size) T2_patch = image.extract_patches_2d(T2_slice, (patch_size, patch_size)) T2_P = T2_patch.reshape(len(Flair_patch), patch_size * patch_size) T_1c_patch = image.extract_patches_2d(T_1c_slice, (patch_size, patch_size)) T1c_P = T_1c_patch.reshape(len(Flair_patch), patch_size * patch_size) temp_patch = np.concatenate([F_P, T1_P, T2_P, T1c_P], axis=1) prediction_slice = predictOutput(temp_patch, activate2, W_list, b_list) # print 'Shape of slice : ', prediction_slice.shape prediction_image.append(prediction_slice) prediction_image = np.array(prediction_image) print 'Prediction shape : ', prediction_image.shape # print 'Slice num : ', slice_num if slice_num == 1: prediction_image = np.transpose(prediction_image, (1, 0, 2)) print 'Look here : ', prediction_image.shape prediction_image = prediction_image.reshape([ xdim - patch_size + 1, ydim - patch_size + 1, zdim, label_num ]) print 'Look after : ', prediction_image.shape output_image = np.zeros([xdim, ydim, zdim, label_num]) output_image[1 + ((patch_size - 1) / 2):xdim - ((patch_size - 1) / 2) + 1, 1 + ((patch_size - 1) / 2):ydim - ((patch_size - 1) / 2) + 1, :] = prediction_image print 'output shape', output_image.shape elif slice_num == 2: prediction_image = prediction_image.reshape([ zdim, ydim - patch_size + 1, xdim - patch_size + 1, label_num ]) output_image = np.zeros([xdim, ydim, zdim, label_num]) output_image[:, 1 + ((patch_size - 1) / 2):ydim - ((patch_size - 1) / 2) + 1, 1 + ((patch_size - 1) / 2):xdim - ((patch_size - 1) / 2) + 1] = prediction_image output_image = np.swapaxes(output_image, 2, 1) output_image = np.swapaxes(output_image, 1, 0) elif slice_num == 3: prediction_image = prediction_image.reshape([ zdim, ydim - patch_size + 1, xdim - patch_size + 1, label_num ]) output_image = np.zeros([zdim, ydim, xdim, label_num]) output_image[:, 1 + ((patch_size - 1) / 2):ydim - ((patch_size - 1) / 2) + 1, 1 + ((patch_size - 1) / 2):xdim - ((patch_size - 1) / 2) + 1, :] = prediction_image # print # np.save(,output_image)#TODO: save it in meaningful name in corresponding folder # break print 'Output shape : ', output_image.shape np.save( Folder[image_iterator] + Subdir_array[image_iterator] + prefix + '.npy', output_image) output_image = np.argmax(output_image, axis=3) a = output_image for j in xrange(a.shape[2]): a[:, :, j] = np.transpose(a[:, :, j]) print 'a shape here: ', a.shape output_image = a # save_image = np.zeros() # output_image = itk_py_converter.GetImageFromArray(a.tolist()) # writer.SetFileName(Folder[image_iterator]+Subdir_array[image_iterator]+'_'+prefix+'_.mha') # writer.SetInput(output_image) # writer.Update() affine = [[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] img = nib.Nifti1Image(output_image, affine) img.set_data_dtype(np.int32) nib.save( img, Folder[image_iterator] + Subdir_array[image_iterator] + prefix + 'xyz.nii') print '########success########'
""" ========================================= Image denoising using dictionary learning ========================================= """ print(__doc__) from time import time import matplotlib.pyplot as plt import numpy as np import scipy as sp from sklearn.decomposition import MiniBatchDictionaryLearning from sklearn.feature_extraction.image import extract_patches_2d from sklearn.feature_extraction.image import reconstruct_from_patches_2d patch_size = (6, 6) data=sp.io.loadmat('C:\\Users\\paulo\\Downloads\\FaceRecogToolv4\\distorted.mat') data=data['distorted']/255 patches = extract_patches_2d(data, patch_size) sp.io.savemat('C:\\Users\\paulo\\Downloads\\FaceRecogToolv4\\patches.mat', {'patches':patches})
def colourMoments(image, skin_mask, window_size=None): if image.ndim > 2: n_colour_channels = image.shape[2] else: n_colour_channels = 1 if window_size is None: # One colour moment per image channel c_moment_mean = np.zeros((1, n_colour_channels)) c_moment_var = np.zeros((1, n_colour_channels)) c_moment_skew = np.zeros((1, n_colour_channels)) c_moment_kurtosis = np.zeros((1, n_colour_channels)) # Compute colour moments over the skin region for j in range(0, n_colour_channels): # Mean c_moment_mean[1, j] = np.mean(image[skin_mask != 0, j]) # Standard deviation c_moment_var[1, j] = np.std(image[skin_mask != 0, j]) # Skewness c_moment_skew[1, j] = \ scipy.stats.skew(image[skin_mask != 0, j].ravel()) # Kurosis c_moment_kurtosis[1, j] = \ scipy.stats.kurtosis(image[skin_mask != 0, j].ravel()) return c_moment_mean, c_moment_var, c_moment_skew, c_moment_kurtosis else: # One colour moment per pixel per channel REDUCED_RES_M = IMG_RES_M - (window_size - 1) REDUCED_RES_N = IMG_RES_N - (window_size - 1) REDUCED_PIXEL_COUNT = REDUCED_RES_M * REDUCED_RES_N PAD_SIZE = np.uint8(np.floor(window_size / 2)) k = np.uint8(np.floor(window_size / 2)) skin_mask_r = skin_mask[k:skin_mask.shape[0] - k, k:skin_mask.shape[1] - k].ravel() patches = extract_patches_2d(image, (window_size, window_size)) if patches.ndim < 4: patches = patches[:, :, :, np.newaxis] c_moment_mean = np.zeros((REDUCED_PIXEL_COUNT, n_colour_channels)) c_moment_var = np.zeros((REDUCED_PIXEL_COUNT, n_colour_channels)) c_moment_skew = np.zeros((REDUCED_PIXEL_COUNT, n_colour_channels)) c_moment_kurtosis = np.zeros((REDUCED_PIXEL_COUNT, n_colour_channels)) # Compute colour moments for i in range(0, len(skin_mask_r)): if skin_mask_r[i] != 0: for j in range(0, n_colour_channels): # Mean c_moment_mean[i, j] = np.mean(patches[i, :, :, j]) # Standard deviation c_moment_var[i, j] = np.std(patches[i, :, :, j]) # Skewness c_moment_skew[i, j] = \ scipy.stats.skew(patches[i, :, :, j].ravel()) # Kurosis c_moment_kurtosis[1, j] = \ scipy.stats.kurtosis(patches[i, :, :, j].ravel()) # Padding c_moment_mean = c_moment_mean.reshape( (REDUCED_RES_M, REDUCED_RES_N, n_colour_channels)) c_moment_mean = np.pad(c_moment_mean, [(PAD_SIZE, PAD_SIZE), (PAD_SIZE, PAD_SIZE), (0, 0)], 'edge') c_moment_var = c_moment_var.reshape( (REDUCED_RES_M, REDUCED_RES_N, n_colour_channels)) c_moment_var = np.pad(c_moment_var, [(PAD_SIZE, PAD_SIZE), (PAD_SIZE, PAD_SIZE), (0, 0)], 'edge') c_moment_skew = c_moment_skew.reshape( (REDUCED_RES_M, REDUCED_RES_N, n_colour_channels)) c_moment_skew = np.pad(c_moment_skew, [(PAD_SIZE, PAD_SIZE), (PAD_SIZE, PAD_SIZE), (0, 0)], 'edge') c_moment_kurtosis = c_moment_kurtosis.reshape( (REDUCED_RES_M, REDUCED_RES_N, n_colour_channels)) c_moment_kurtosis = np.pad(c_moment_kurtosis, [(PAD_SIZE, PAD_SIZE), (PAD_SIZE, PAD_SIZE), (0, 0)], 'edge') return c_moment_mean, c_moment_var, c_moment_skew, c_moment_kurtosis
def main(): image_files = glob.glob(IMAGE_DIR + '/*/**.[jJ][pP][gG]') image_files.sort() aug_image_files = glob.glob(AUG_IMAGE_DIR + '/*/**.[jJ][pP][gG]') aug_image_files.sort() image_files = zip(image_files, aug_image_files) sk_files = glob.glob(SK_DIR + '/*/**.png') sk_files.sort() aug_sk_files = glob.glob(AUG_SK_DIR + '/*/**.png') aug_sk_files.sort() sk_files = zip(sk_files, aug_sk_files) _writeDataset(image_files, sk_files, DATASET_CSV) X = [] with open(DATASET_CSV, 'r', newline='') as data_file: data_reader = csv.DictReader(data_file, delimiter=',') data_list = list(data_reader) Y = data_list['ery_score'] for i in range(0, len(data_list)): image = cv2.imread(data_list[i]['img_file'], cv2.IMREAD_COLOR) skin_mask = cv2.imread(data_list[i]['sk_file'], cv2.IMREAD_UNCHANGED) skin_mask = np.uint8(skin_mask / 255) image_m, image_n, _ = image.shape if (image_m != 512 or image_n != 512): image = cv2.resize(image, (512, 512)) skin_mask = cv2.resize(skin_mask, (512, 512)) # Colour constancy image = imgnorm.greyWorld(image, median_blur=3, norm=6) image_LAB = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) # Colour moments c1, c2, c3, c4 = colourMoments(image_LAB[:, :, :], skin_mask) # Re-quantisation of A channels image_A_q = uniformQuantise(image_LAB[:, :, 1], 64) # plt.imshow(image_A_q) # plt.colorbar() skin_segment = _skinSegment(image_A_q, skin_mask) lesion_mask = kmeans(image_LAB[skin_mask != 0, 1], skin_mask) n_lesion_pixels = np.count_nonzeros(lesion_mask) n_skin_pixels = np.count_nonzeros(skin_segment) coverage = n_lesion_pixels / n_skin_pixels X_temp = np.concatenate([c1, c2, c3, c4, coverage], axis=None) # Extract 3 random patches from the images good_patch_count = 0 count = 0 patches = extract_patches_2d(skin_segment, (32, 32), max_patches=100, random_state=1) for patch in patches: if good_patch_count == 4: break if count < 50: if (32 * 32 - np.count_nonzero(patch) < 103): good_patch_count = good_patch_count + 1 count = count + 1 g1, g2, g3, g4 = glcm(patch, skin_mask, 64) X_temp = np.concatenate((X_temp, [g1, g2, g3, g4]), axis=None) if count > 50: if (32 * 32 - np.count_nonzero(patch) < 206): good_patch_count = good_patch_count + 1 count = count + 1 g1, g2, g3, g4 = glcm(patch, skin_mask, 64) X_temp = np.concatenate((X_temp, [g1, g2, g3, g4]), axis=None) X.append(X_temp) joblib.dump(np.array(X), 'X_data.joblib', 3) # X = joblib.load('X_data.joblib') X_train, X_test, Y_train, Y_test = train_test_split(X, Y, random_state=1, shuffle=True, stratify=Y) kNN_clf = KNeighborsClassifier(n_neighbors=5, weights='uniform', algorithm='auto', leaf_size=30, p=2, metric='minkowski', metric_params=None, n_jobs=10) kNN_clf.fit(X_train, Y_train) y = kNN_clf.predict(X_test) bc_ACC = balanced_accuracy_score(Y_test, y) print(bc_ACC)
def create_feature_arr(featdir, file_arr): features = [] classes = [] for textr_file in file_arr: basename = os.path.basename(textr_file) idx = basename.find('_orig') img_name = basename[0:idx] mask_file = os.path.join(featdir, img_name + '_mask.png') texture = np.load(textr_file) #texture arr format is [p,30,30,2] where # p is the number of patches in the image and 2 is the number of texture features mask = io.imread(mask_file) mask_patches = extract_patches_2d(mask, (30, 30)) #break mask into patches X = np.mean(texture, axis=(1, 2)) y = np.mean(mask_patches, axis=(1, 2)) y[y < 100] = 0 #make sure y is [0,1] y[y > 0] = 1 ### balance data ### idx_fore = np.nonzero(y == 1)[0] nFore = len(idx_fore) idx_back = np.nonzero(y == 0)[0] nBack = len(idx_back) if nBack > nFore: rnd_idx = rnd_list(0, nBack - 1, nFore) new_idx_back = idx_back[ rnd_idx] #randomly select nFore indices from background patches X_back = X[new_idx_back, ...] X_fore = X[idx_fore] y_back = y[new_idx_back] y_fore = y[idx_fore] X = np.concatenate((X_back, X_fore), axis=0) y = np.concatenate((y_back, y_fore), axis=0) elif nBack < nFore: rnd_idx = rnd_list(0, nFore - 1, nBack) new_idx_fore = idx_fore[ rnd_idx] #randomly select nFore indices from background patches X_back = X[idx_back, ...] X_fore = X[new_idx_fore] y_back = y[idx_back] y_fore = y[new_idx_fore] X = np.concatenate((X_back, X_fore), axis=0) y = np.concatenate((y_back, y_fore), axis=0) #shuffle final feature and classes array nSamples = X.shape[0] new_idx = np.arange(nSamples) np.random.shuffle(new_idx) X = X[new_idx, ...] y = y[new_idx] if features == [] and classes == []: features = X classes = y else: features = np.concatenate((features, X), axis=0) classes = np.concatenate((classes, y), axis=0) return features, classes
def lpsd_dist_p(self, data, dist_num, is_patch=True): data = data.tolist() chunk_list = utils.chunkIt(data, dist_num) data_list = [] for item in chunk_list: if len(item) > 1: data_list.append(item) while True: queue_list = [] procs = [] for queue_val in queue_list: print(queue_val.empty()) for i, data in enumerate(data_list): queue_list.append(Queue( )) # define queues for saving the outputs of functions procs.append( Process(target=self.get_lpsd_p, args=(data, queue_list[i]))) # define process for queue_val in queue_list: print(queue_val.empty()) for p in procs: # process start p.start() for queue_val in queue_list: while queue_val.empty(): pass for queue_val in queue_list: print(queue_val.empty()) M_list = [] for i in range( dist_num): # save results from queues and close queues # while not queue_list[i].empty(): get_time = time.time() M_list.append(queue_list[i].get(timeout=3)) get_time = time.time() - get_time queue_list[i].close() queue_list[i].join_thread() for p in procs: # close process p.terminate() if get_time < 3: break else: print('Some error occurred, restarting the lpsd extraction...') result = np.concatenate(M_list, axis=0) # result = np.asarray(M_list) # print(result.shape) # result = np.reshape(result, (-1, result.shape[2], result.shape[3])) lpsd = np.expand_dims( result[:, :, 0], axis=2 ) # expand_dims for normalization (shape matching for broadcast) if not self._is_training: self.phase.append(result[:, :, 1]) pad = np.expand_dims(np.zeros( (int(config.time_width / 2), lpsd.shape[1])), axis=2) # pad for extracting the patches if is_patch: mean, std = self.norm_process(self._norm_dir + '/norm_noisy.mat') lpsd = self._normalize(mean, std, lpsd) else: mean, std = self.norm_process(self._norm_dir + '/norm_noisy.mat') lpsd = self._normalize(mean, std, lpsd) # print(result.shape) if is_patch: lpsd = np.squeeze(np.concatenate((pad, lpsd, pad), axis=0)) # padding for patching # print(result.shape) lpsd = image.extract_patches_2d(lpsd, (config.time_width, lpsd.shape[1])) lpsd = np.expand_dims(lpsd, axis=3) return lpsd
from sklearn.feature_extraction import image as sk_image import tensorflow as tf a = np.expand_dims(np.random.uniform(0, 1, 300), axis=0) a = np.reshape(a, (10, 10, 3), order='F') a = np.transpose(a, (1, 0, 2)) b = np.expand_dims(np.random.uniform(0, 1, 300), axis=0) b = np.reshape(b, (10, 10, 3), order='F') b = np.transpose(b, (1, 0, 2)) bb = tf.expand_dims(tf.expand_dims(tf.convert_to_tensor(b, np.float32), axis=0), axis=4) features_bank = sk_image.extract_patches_2d(a, (3, 3)) features_bank = np.expand_dims(np.transpose(features_bank, (1, 2, 3, 0)), axis=3) # Calculate normalized correlations layer_filtered = tf.nn.conv3d(bb, features_bank, strides=[1, 1, 1, 1, 1], padding='VALID') max_filter_response_idx = tf.squeeze(tf.argmax(layer_filtered, axis=4)) max_filter_response_idx_ = tf.reshape(max_filter_response_idx, [-1]) max_filter_response_weight = tf.squeeze( tf.reduce_max(tf.abs(layer_filtered), axis=4)) max_filter_response_weight = tf.reshape(max_filter_response_weight, [-1]) max_filter_response_weight = max_filter_response_weight / tf.reduce_max( max_filter_response_weight)
unknown = cv2.subtract(sure_bg, sure_fg) ret, markers = cv2.connectedComponents(sure_fg) markers = markers + 1 markers[unknown == 255] = 0 markers = cv2.watershed(x, markers) x[markers == -1] = [255, 0, 0] B = x B = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY) th1 = cv2.adaptiveThreshold(B, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) th2 = cv2.adaptiveThreshold(B, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) patches = image.extract_patches_2d( th1, (32, 32), 30, ) patches = patches.reshape(len(patches), np.prod(patches.shape[1:])) p.append(patches) print(len(p)) print(len(p[0])) p1 = [] path = r'C:\Users\ANIKET KUMAR\Desktop\iitbhu_intern\DATASET\fold3\test\separated200x\Malignant' files = [f for f in listdir(path) if isfile(join(path, f))] for image_file in files: x = cv2.imread(os.path.join(path, image_file)) #print(x.shape) x = cv2.resize(x, (350, 230)) gray = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
dst_size=tuple(conf["window_dim"])) rois = (roi, cv2.flip(roi, 1)) if conf["use_flip"] else (roi, ) for roi in rois: features = hog.describe(roi) data.append(features) labels.append(1) pbar.update(i) pbar.finish() # grab the negative images negative_sample_paths = list(paths.list_images(conf["image_distractions"])) pbar = progressbar.ProgressBar(maxval=conf["num_distraction_images"], widgets=widgets).start() print("[INFO] describing distraction ROIs...") for i in np.arange(0, conf["num_distraction_images"]): image = cv2.imread(random.choice(negative_sample_paths)) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) patches = extract_patches_2d(image, tuple(conf["window_dim"]), max_patches=conf["num_distractions_per_image"]) for patch in patches: features = hog.describe(patch) data.append(features) labels.append(-1) pbar.update(i) pbar.finish() print("[INFO] dumping features and labels to file...") dataset.dump_dataset(data, labels, conf["features_path"], "features")
pwidth = 128 max_patches = 1 # number of patches for each day random_state = 555 #for reproductivity window_shape = (pheight, pwidth) nbdaysTrain = 5100 #number of considered days from the available training days strideDays = 1 #strideDays*nbdaysTrain should be less than the number of the available training images here 5116 nbpatchs = max_patches x_train = np.zeros((nbdaysTrain * nbpatchs, 1, pheight, pwidth)) BB_label = np.zeros((nbdaysTrain * nbpatchs, pheight, pwidth)).astype(int) for dayN in range(nbdaysTrain): x_train[dayN * nbpatchs:dayN * nbpatchs + nbpatchs, 0, :, :] = extract_patches_2d( SSH_aviso_train[0:128, :, strideDays * dayN + 1], window_shape, max_patches, random_state + dayN) BB_label[dayN * nbpatchs:dayN * nbpatchs + nbpatchs, :, :] = extract_patches_2d( SegmaskTot[0:128, :, strideDays * dayN], window_shape, max_patches, random_state + dayN) ### x_train[ x_train < -100] = 0. ##### change the standard AVISO fill value to zero BB_label[BB_label == 3] = 0. ##### class 3 is class 0 in this work label_train = np.reshape(BB_label, (len(x_train), 1, pheight * pwidth)).transpose( (0, 2, 1)) x_train_label = np.zeros((len(x_train), pheight * pwidth, nbClass))
def extractPatches(im, patch_size): return image.extract_patches_2d(im, (patch_size * 2, patch_size * 2), max_patches=96)
def preprocess(self, image): #extract a random crop from the image with the target height and width return extract_patches_2d( image, patch_size=(self.height, self.width), max_patches=1)[0]
def make_patches(x, scale, patch_size, upscale=True, verbose=1): '''x shape: (num_channels, rows, cols)''' height, width = x.shape[:2] if upscale: x = imresize(x, (height * scale, width * scale)) patches = extract_patches_2d(x, (patch_size, patch_size)) return patches
def __getitem__(self, index): # random crop if self.args.noise_type == 'Gaussian' or self.args.noise_type == 'Poisson-Gaussian': clean_img = self.clean_arr[index, :, :] noisy_img = self.noisy_arr[index, :, :] if self.args.data_type == 'Grayscale': rand = random.randrange(1, 10000) clean_patch = image.extract_patches_2d( image=clean_img, patch_size=(self.args.crop_size, self.args.crop_size), max_patches=1, random_state=rand) noisy_patch = image.extract_patches_2d( image=noisy_img, patch_size=(self.args.crop_size, self.args.crop_size), max_patches=1, random_state=rand) # Random horizontal flipping if random.random() > 0.5: clean_patch = np.fliplr(clean_patch) noisy_patch = np.fliplr(noisy_patch) # Random vertical flipping if random.random() > 0.5: clean_patch = np.flipud(clean_patch) noisy_patch = np.flipud(noisy_patch) else: rand_x = random.randrange( 0, (clean_img.shape[0] - self.args.crop_size - 1) // 2) rand_y = random.randrange( 0, (clean_img.shape[1] - self.args.crop_size - 1) // 2) clean_patch = clean_img[rand_x * 2:rand_x * 2 + self.args.crop_size, rand_y * 2:rand_y * 2 + self.args.crop_size].reshape( 1, self.args.crop_size, self.args.crop_size) noisy_patch = noisy_img[rand_x * 2:rand_x * 2 + self.args.crop_size, rand_y * 2:rand_y * 2 + self.args.crop_size].reshape( 1, self.args.crop_size, self.args.crop_size) if self.args.loss_function == 'MSE': source = torch.from_numpy(noisy_patch.copy()) target = torch.from_numpy(clean_patch.copy()) return source, target elif self.args.loss_function == 'MSE_Affine' or self.args.loss_function == 'N2V' or self.args.loss_function == 'Noise_est' or self.args.loss_function == 'EMSE_Affine': source = torch.from_numpy(noisy_patch.copy()) target = torch.from_numpy(clean_patch.copy()) target = torch.cat([source, target], dim=0) return source, target else: ## real data return source, target