def _process(bytes, *, net, transformer): """ Process an image using a Caffe network and transformer. Parameters ---------- bytes : a resized image as a BytesIO object net : an instance of caffe.Net transformer : an instance of caffe.io.Transformer Returns ------- probabilities : an array of SFW and NSFW probabilities as floats """ loaded_image = load_image(bytes) layers = ["prob"] H, W, _ = loaded_image.shape _, _, h, w = net.blobs["data"].data.shape h_off = int(max((H - h) / 2, 0)) w_off = int(max((W - w) / 2, 0)) cropped_image = loaded_image[h_off:h_off + h, w_off:w_off + w, :] transformed_image = transformer.preprocess("data", cropped_image) transformed_image.shape = (1,) + transformed_image.shape output = net.forward_all(blobs=layers, **{ net.inputs[0]: transformed_image }) return output[layers[0]][0].astype(float)
def load_next_image(self): '''load next image in same batchsize''' if self._cur == len(self.indexlist): self._cur = 0 shuffle(self.indexlist) image_1, image_2, sim = self.indexlist[ self._cur][:-1] #extract one line and remove '\n' data_1 = load_image(osp.join(self.imagepath, image_1)) data_2 = load_image(osp.join(self.imagepath, image_2)) try: data_1 = self.preprocess(data_1) data_2 = self.preprocess(data_2) except: print osp.join(self.imagepath, image_1) print osp.join(self.imagepath, image_2) self._cur += 1 return data_1, data_2, sim
def classify(): file = request.files['file'] if file: img_data = file.read() orig_img = load_image(StringIO(img_data), color=False) img = preprocess_image(orig_img) ret = send_image(img) return render_template('result.html', result=ret, img=base64.b64encode(img_data)) return 'Upload failed!'
def load_next_image(self): '''load next image in same batchsize''' if self._cur == len(self.indexlist): self._cur = 0 shuffle(self.indexlist) line = self.indexlist[self._cur][:-1] #remove '\n' imagename = line.split()[0] labels = line.split()[1:] image = load_image(osp.join(self.imagepath, imagename)) try: image = self.preprocess(image) except: print osp.join(self.imagepath, imagename) self._cur += 1 return image, labels
values.count(4)) * 140 patches = np.zeros((total_numPatches, rfSize * rfSize * 3)) whitening = True maxIter = 50 batchSize = 1000 j = 0 values = labels_dict.values() for each in images: if labels_dict[each.split('.')[0]] > 0: numPatches = 140 else: numPatches = 40 img = load_image('../data/resized/trainOriginal/' + each) windows = view_as_windows(img, (rfSize, rfSize, 3)) r = np.random.randint(0, windows.shape[0] - windows.shape[3], (numPatches, )) c = np.random.randint(0, windows.shape[1] - windows.shape[4], (numPatches, )) for i in range(0, numPatches): patch = np.reshape( windows[r[i], c[i], 0, :], windows.shape[3] * windows.shape[4] * windows.shape[5]) patches[j, :] = patch if j % 100 == 0: print "Extracted {0}th patch of {1} patches totally".format( j, total_numPatches) j += 1
def extract_features(path, keys, centroids, rfSize, ImageDim, whitening, M, P, stride): #assert(nargin == 4 || nargin == 6) numCentroids = centroids.shape[0] numSamples = len(keys) numFeats = ImageDim[0] * ImageDim[1] * ImageDim[2] # compute features for all training images XC = np.zeros((numSamples, numCentroids * 4)) labels = np.zeros((numSamples, 1)) j = 0 for i in range(numSamples): total_start = time.time() print "Sample {0}".format(i) if (np.mod(i, 2000) == 0): np.save( 'test_features_windowsize_{0}_iteration_{1}'.format(rfSize, i), XC) print 'Extracting features: ' + str(i) + '/' + str(numSamples) img = load_image(path + keys[i]) # X = np.transpose(reader.next()[1]).flatten() X = img.flatten() # extract overlapping sub-patches into rows of 'patches' start = time.time() patches = np.vstack( (im2col(np.reshape(X[0:numFeats / 3], ImageDim[0:2], 'F'), (rfSize, rfSize), stride), im2col( np.reshape(X[numFeats / 3:numFeats * 2 / 3], ImageDim[0:2], 'F'), (rfSize, rfSize), stride), im2col( np.reshape(X[numFeats * 2 / 3:numFeats], ImageDim[0:2], 'F'), (rfSize, rfSize), stride))).T end = time.time() w = view_as_windows(img, (rfSize, rfsize, 3), stride) w = w.reshape( (w.shape[0] * w.shape[1], w.shape[3] * w.shape[4] * w.shape[5])) print np.array_equal(patches, w) from time import sleep for i in xrange(w.shape[0]): print w[i, 0:20] print patches[i, 500:520] sleep(1) print "Extract overlapping sub-patches time:{0}".format(end - start) # do preprocessing for each patch # normalize for contrast start = time.time() patchesMean = np.mean(patches, axis=1, dtype=np.float32, keepdims=True) patchesVar = np.var(patches, axis=1, dtype=np.float32, ddof=1, keepdims=True) offsetMatrix = 10.0 * np.ones(patchesVar.shape) patches = (patches - patchesMean) / np.sqrt(patchesVar + offsetMatrix) end = time.time() print "Preprocessing time:{0}".format(end - start) # whiten if (whitening): patches = np.dot((patches - M), P) # compute 'triangle' activation function start = time.time() z = native_cdist(patches, centroids) end = time.time() print "Triangle time:{0}".format(end - start) start = time.time() mu = np.tile( np.array([np.mean(z, axis=1)]).T, (1, centroids.shape[0] )) # average distance to centroids for each patch patches = np.maximum(mu - z, np.zeros(mu.shape)) end = time.time() print "Distance calculation time:{0}".format(end - start) # patches is now the data matrix of activations for each patch # reshape to numCentroids-channel image start = time.time() prows = (ImageDim[0] - rfSize + 1 * stride) / stride pcols = (ImageDim[1] - rfSize + 1 * stride) / stride patches = np.reshape(patches, (prows, pcols, numCentroids), 'F') end = time.time() print "Reshaping time:{0}".format(end - start) start = time.time() # pool over quadrants halfr = np.round(float(prows) / 2) halfc = np.round(float(pcols) / 2) q1 = np.array( [np.sum(np.sum(patches[0:halfc, 0:halfr, :], axis=1), axis=0)]) q2 = np.array([ np.sum(np.sum(patches[halfc:patches.shape[0], 0:halfr, :], axis=1), axis=0) ]) q3 = np.array([ np.sum(np.sum(patches[0:halfc, halfr:patches.shape[1], :], axis=1), axis=0) ]) q4 = np.array([ np.sum(np.sum(patches[halfc:patches.shape[0], halfr:patches.shape[1], :], axis=1), axis=0) ]) end = time.time() print "Pooling time:{0}".format(end - start) # concatenate into feature vector XC[j, :] = np.vstack((q1, q2, q3, q4)).flatten() j += 1 total_end = time.time() print "Iteration time:{0}".format(total_end - total_start) return XC
total_numPatches = values.count(0)*40 + (values.count(1) + values.count(2) + values.count(3) + values.count(4)) * 140 patches = np.zeros((total_numPatches, rfSize*rfSize*3)) whitening = True maxIter = 50 batchSize = 1000 j = 0 values = labels_dict.values() for each in images: if labels_dict[each.split('.')[0]] > 0: numPatches = 140 else: numPatches = 40 img = load_image('../data/resized/trainOriginal/' + each) windows = view_as_windows(img, (rfSize, rfSize, 3)) r = np.random.randint(0, windows.shape[0] - windows.shape[3], (numPatches,)) c = np.random.randint(0, windows.shape[1]- windows.shape[4], (numPatches,)) for i in range(0, numPatches): patch = np.reshape(windows[r[i],c[i],0,:], windows.shape[3]*windows.shape[4]*windows.shape[5]) patches[j,:] = patch if j % 100 == 0: print "Extracted {0}th patch of {1} patches totally".format(j,total_numPatches) j += 1 numCentroids = int(np.sqrt(total_numPatches/2)) patchesMean = np.mean(patches, axis=1, dtype=np.float32, keepdims=True) patchesVar = np.var(patches, axis=1, dtype=np.float32, keepdims=True) offsetMatrix = 10.0 * np.ones(patchesVar.shape)
def extract_features(path, keys, centroids, rfSize, ImageDim, whitening, M, P, stride): #assert(nargin == 4 || nargin == 6) numCentroids = centroids.shape[0] numSamples = len(keys) numFeats = ImageDim[0]*ImageDim[1]*ImageDim[2] # compute features for all training images XC = np.zeros((numSamples, numCentroids*4)) labels = np.zeros((numSamples, 1)) j = 0 for i in range(numSamples): total_start = time.time() print "Sample {0}".format(i) if (np.mod(i,2000) == 0): np.save('test_features_windowsize_{0}_iteration_{1}'.format(rfSize,i), XC) print 'Extracting features: ' + str(i) + '/' + str(numSamples) img = load_image(path + keys[i]) # X = np.transpose(reader.next()[1]).flatten() X = img.flatten() # extract overlapping sub-patches into rows of 'patches' start = time.time() patches = np.vstack( (im2col(np.reshape(X[0:numFeats/3],ImageDim[0:2],'F'), (rfSize, rfSize), stride), im2col(np.reshape(X[numFeats/3:numFeats*2/3],ImageDim[0:2],'F'), (rfSize, rfSize), stride), im2col(np.reshape(X[numFeats*2/3:numFeats],ImageDim[0:2],'F'), (rfSize, rfSize), stride))).T end = time.time() w = view_as_windows(img, (rfSize,rfsize, 3), stride) w = w.reshape((w.shape[0]*w.shape[1],w.shape[3]*w.shape[4]*w.shape[5])) print np.array_equal(patches, w) from time import sleep for i in xrange(w.shape[0]): print w[i,0:20] print patches[i,500:520] sleep(1) print "Extract overlapping sub-patches time:{0}".format(end - start) # do preprocessing for each patch # normalize for contrast start = time.time() patchesMean = np.mean(patches, axis=1, dtype=np.float32, keepdims=True) patchesVar = np.var(patches, axis=1, dtype=np.float32, ddof=1, keepdims=True) offsetMatrix = 10.0 * np.ones(patchesVar.shape) patches = (patches - patchesMean) / np.sqrt(patchesVar + offsetMatrix) end = time.time() print "Preprocessing time:{0}".format(end - start) # whiten if (whitening): patches = np.dot((patches - M), P) # compute 'triangle' activation function start = time.time() z = native_cdist(patches, centroids) end = time.time() print "Triangle time:{0}".format(end - start) start = time.time() mu = np.tile(np.array([np.mean(z, axis = 1)]).T, (1, centroids.shape[0])) # average distance to centroids for each patch patches = np.maximum(mu - z, np.zeros(mu.shape)) end = time.time() print "Distance calculation time:{0}".format(end - start) # patches is now the data matrix of activations for each patch # reshape to numCentroids-channel image start = time.time() prows = (ImageDim[0]-rfSize + 1*stride)/stride pcols = (ImageDim[1]-rfSize + 1*stride)/stride patches = np.reshape(patches, (prows, pcols, numCentroids),'F') end = time.time() print "Reshaping time:{0}".format(end - start) start = time.time() # pool over quadrants halfr = np.round(float(prows)/2) halfc = np.round(float(pcols)/2) q1 = np.array([np.sum(np.sum(patches[0:halfc, 0:halfr, :], axis = 1),axis = 0)]) q2 = np.array([np.sum(np.sum(patches[halfc:patches.shape[0], 0:halfr, :], axis = 1),axis = 0)]) q3 = np.array([np.sum(np.sum(patches[0:halfc, halfr:patches.shape[1], :], axis = 1),axis = 0)]) q4 = np.array([np.sum(np.sum(patches[halfc:patches.shape[0], halfr:patches.shape[1], :], axis = 1),axis = 0)]) end = time.time() print "Pooling time:{0}".format(end - start) # concatenate into feature vector XC[j,:] = np.vstack((q1,q2,q3,q4)).flatten() j += 1 total_end = time.time() print "Iteration time:{0}".format(total_end - total_start) return XC
from shutil import copyfile # parameters generator_name = 'deepsim-conv4' generator = Generators.get_generator(generator_name) srcdirs = ('imgs', ) # to be changed; list of directories containing source images dstrootdir = os.path.join('codes', generator_name) # main for srcdir in srcdirs: leafdir = os.path.basename(srcdir) dstdir = os.path.join(dstrootdir, leafdir) if not os.path.isdir(dstdir): os.makedirs(dstdir) imgfns = [ fn for fn in os.listdir(srcdir) if len(fn) > 3 and fn[-4:] == '.bmp' ] for imgfn in imgfns: im = load_image(os.path.join(srcdir, imgfn)) if generator.name == 'raw_pixel': code = generator.encode(im) else: code = generator.encode(im, steps=200) np.save(os.path.join(dstdir, imgfn[:-4] + '.npy'), code, allow_pickle=False) imwrite(os.path.join(dstdir, imgfn[:-4] + '_syn.png'), generator.visualize(code)[:, :, ::-1]) copyfile(os.path.join(srcdir, imgfn), os.path.join(dstdir, imgfn))