def channels(img): # 32 * 32 * c img_luv = BGR2CIELUV(img) l, u, v = cv2.split(img_luv / 255) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) / 255.0 ds_img_gray = downsample_keepdim(img_gray) mag, c1, c2, c3, c4 = gradient_magnitude(img_gray) ds_mag, ds_c1, ds_c2, ds_c3, ds_c4 = gradient_magnitude(ds_img_gray) merged = cv2.merge( (l, u, v, mag, c1, c2, c3, c4, ds_mag, ds_c1, ds_c2, ds_c3, ds_c4)) ds_merged = downsample(merged, 2) # print(ds_merged.shape) blur_ds = bartlett_blurring(ds_merged, 8) blur_ds = downsample(blur_ds, 16 / 5).reshape((25, -1)) # print(blur_ds.shape) additional_length = blur_ds.shape[0] * (blur_ds.shape[0] - 1) / 2 channel_length = ds_merged.shape[0] * ds_merged.shape[1] * ds_merged.shape[ 2] newfeature = np.zeros( int(additional_length * blur_ds.shape[1] + channel_length)) newfeature[:int(ds_merged.shape[0] * ds_merged.shape[1] * ds_merged.shape[2])] = ds_merged.flatten() for i in range(blur_ds.shape[1]): newfeature[int(i * additional_length + channel_length):int((i + 1) * additional_length + channel_length)] = pdist( blur_ds[:, i][:, np.newaxis]) return newfeature
def decode(self, wavfile, offsets=False): ''' Takes in a wavfile and returns an list of Annotations (see utils). :param wavfile: :param offsets: :return: ''' devnull = open(os.devnull, 'w') starting_directory = os.path.dirname(wavfile) # create directories downsampled_8k_directory = os.path.join(os.path.dirname(wavfile), '8k_wavfiles/') self.dir_8k = downsampled_8k_directory if not os.path.exists(downsampled_8k_directory): os.makedirs(downsampled_8k_directory) # basenames etc base = os.path.basename(wavfile) self.no_ext = os.path.splitext(wavfile)[0] base_no_ext = os.path.splitext(base)[0] wavfile_8k = os.path.join(downsampled_8k_directory, base) utils.downsample(wavfile, wavfile_8k, 8000) self.wavfile_8k = wavfile_8k assert os.path.exists(wavfile_8k) raw_output = self._core_asr(wavfile_8k) self.kaldi_transcription = self._read_transcription( raw_output, offsets) return self.kaldi_transcription
def apply_mask_to_image(): """ Input: -Image Original -Image mask (from get_model_unet()) Output -Return a bitwise image applied a applied be """ # print("apply_mask_endpoint", file=sys.stderr) image_orig = request.args.get('orig') image_mask = request.args.get('mask') # 1.- convert base64 uri o png image and save to tmp folder image_name_orig = convert_and_save4(image_orig) image_name_mask = convert_and_save4(image_mask) # 2.- Load image to numpy array img_orig = imread(image_name_orig) img_mask = imread(image_name_mask) # debug_np_to_file(img_mask, '/tmp/mask.jpg') # 3.- resize image to model specs 256x256 resized_image_orig = np.uint8(downsample(img_orig, H, W)) resized_image_mask = np.uint8(downsample(img_mask, H, W)) debug_np_to_file(resized_image_mask, '/tmp/resized_mask.jpg') debug_np_to_file(resized_image_orig, '/tmp/resized_orig.jpg') # 4.- perform bitwise operation to extract car appling mask final = get_clean_image(resized_image_orig, resized_image_mask) debug_np_to_file(final, '/tmp/car.jpg') ret = save_np_array_to_image(final) return ret
def Discriminator(): initializer = tf.random_normal_initializer(0., 0.02) inp = tf.keras.layers.Input(shape=[None, None, 3], name='input_image') tar = tf.keras.layers.Input(shape=[None, None, 3], name='target_image') x = tf.keras.layers.concatenate([inp, tar]) # (bs, 256, 256, channels*2) down1 = downsample(64, 4, False)(x) # (bs, 128, 128, 64) down2 = downsample(128, 4)(down1) # (bs, 64, 64, 128) down3 = downsample(256, 4)(down2) # (bs, 32, 32, 256) zero_pad1 = tf.keras.layers.ZeroPadding2D()(down3) # (bs, 34, 34, 256) conv = tf.keras.layers.Conv2D(512, 4, strides=1, kernel_initializer=initializer, use_bias=False)( zero_pad1) # (bs, 31, 31, 512) batchnorm1 = tf.keras.layers.BatchNormalization()(conv) leaky_relu = tf.keras.layers.LeakyReLU()(batchnorm1) zero_pad2 = tf.keras.layers.ZeroPadding2D()( leaky_relu) # (bs, 33, 33, 512) last = tf.keras.layers.Conv2D(1, 4, strides=1, kernel_initializer=initializer)( zero_pad2) # (bs, 30, 30, 1) return tf.keras.Model(inputs=[inp, tar], outputs=last)
def parse_inner_coords(regcode,road,river,rail): res = set() cnt = 0 if road: geo = geojson.load(codecs.open(os.path.join(road_path, ADMIN_DICT[regcode]+'.geojson'),'r','latin-1')) for e in geo['features']: line = e['geometry']['coordinates'] l1 = len(line) line = utils.downsample(line,0.003) cnt += (len(line)-l1) if len(line)>9: for c in range(1,len(line)-1): res.add(tuple(line[c])) else: for c in line: res.add(tuple(c)) else: geo = geojson.load(codecs.open(os.path.join(road_path, ADMIN_DICT[regcode]+'_min.geojson'),'r','latin-1')) for e in geo['features']: line = e['geometry']['coordinates'] l1 = len(line) line = utils.downsample(line,0.003) cnt += (len(line)-l1) if len(line)>9: for c in range(1,len(line)-1): res.add(tuple(line[c])) else: for c in line: res.add(tuple(c)) if river: geo = geojson.load(codecs.open(os.path.join(river_path, ADMIN_DICT[regcode]+'.geojson'),'r','latin-1')) for e in geo['features']: line = e['geometry']['coordinates'] l1 = len(line) line = utils.downsample(line,0.001) cnt += (len(line)-l1) if len(line)>9: for c in range(1,len(line)-1): res.add(tuple(line[c])) else: for c in line: res.add(tuple(c)) if rail: geo = geojson.load(codecs.open(os.path.join(rail_path, ADMIN_DICT[regcode]+'.geojson'),'r','latin-1')) for e in geo['features']: line = e['geometry']['coordinates'] l1 = len(line) line = utils.downsample(line,0.001) cnt += (len(line)-l1) if len(line)>9: for c in range(1,len(line)-1): res.add(tuple(line[c])) else: for c in line: res.add(tuple(c)) # print cnt return [list(x) for x in list(res)]
def Generator(): # For convolutions arithmetics see: https://arxiv.org/abs/1603.07285 down_stack = [ downsample(64, 4, apply_batch_norm=False, strides=3, name='down_0'), # (bs, 8, 8, 64) downsample(128, 4, name='down_1'), # (bs, 4, 4, 128) downsample(256, 3, strides=1, name='down_2'), # (bs, 4, 4, 256) downsample(512, 3, name='down_3'), # (bs, 2, 2, 512) downsample(512, 4, name='down_4'), # (bs, 1, 1, 512) ] up_stack = [ upsample(512, 4, apply_dropout=True, name='up_0' ), # (bs, 2, 2, 512) - after concatenation (bs, 2, 2, 1024) upsample(256, 4, apply_dropout=True, name='up_1' ), # (bs, 4, 4, 256) - after concatenation (bs, 4, 4, 512) upsample(128, 4, apply_dropout=True, strides=1, name='up_2' ), # (bs, 4, 4, 128) - after concatenation (bs, 4, 4, 256) upsample(64, 2, apply_dropout=True, strides=2, name='up_3', padding='valid' ), # (bs, 8, 8, 64) - after concatenation (bs, 8, 8, 128) ] initializer = tf.random_normal_initializer(0., 0.02) last = tf.keras.layers.Conv2DTranspose( OUTPUT_CHANNELS, 8, strides=2, padding='valid', kernel_initializer=initializer, activation='tanh') # (bs, 22, 22, 3) concat = tf.keras.layers.Concatenate() inputs = tf.keras.layers.Input(shape=[None, None, 3]) x = inputs # Downsampling through the model skips = [] for down in down_stack: x = down(x) skips.append(x) skips = reversed(skips[:-1]) # Upsampling and establishing the skip connections for up, skip in zip(up_stack, skips): x = up(x) x = concat([x, skip]) x = last(x) return tf.keras.Model(inputs=inputs, outputs=x)
def main(): img_folder = 'data/training/img/' label_folder = 'data/training/label/' test_img_folder = 'data/testing/img/' img_slices_folder = 'data/training/slices/img/' label_slices_folder = 'data/training/slices/label/' test_img_slices_folder = 'data/testing/slices/img/' spleen_slices = [] for filename in os.listdir(img_folder): data = nib.load(img_folder + filename) img = data.get_fdata() hdr = data.header img = downsample(img, 2) img_id = filename[0:-7] for s in range(np.shape(img)[2]): save(img[:, :, s], img_slices_folder + img_id + '_' + str(s) + '.nii.gz', hdr) for filename in os.listdir(test_img_folder): data = nib.load(test_img_folder + filename) img = data.get_fdata() hdr = data.header img = downsample(img, 2) img_id = filename[0:-7] for s in range(np.shape(img)[2]): save(img[:, :, s], test_img_slices_folder + img_id + '_' + str(s) + '.nii.gz', hdr) for filename in os.listdir(label_folder): data = nib.load(label_folder + filename) label = data.get_fdata() hdr = data.header label = downsample(label, 2) label = isolate_spleen(label) label_id = filename[0:-7] for s in range(np.shape(label)[2]): save(label[:, :, s], label_slices_folder + label_id + '_' + str(s) + '.nii.gz', hdr) is_spleen = int(label[:, :, s].any()) if is_spleen: spleen_slices.append( 2.92 ) #weighted probability to ensure 66% chance of drawing spleen else: spleen_slices.append( 0.43 ) #weighted probability to ensure 33% chance of drawing not spleen with open('spleen_probs.txt', 'w', newline='') as f: f.write(str(spleen_slices))
def show_plots(self): n = 30000 plt.plot(utils.downsample(self.__stream, n), color='green') plt.plot(utils.downsample(self.__stream_predicted, n), color='blue', alpha=0.9) plt.plot(utils.downsample(self.__stream_prediction_error, n), color='red', alpha=0.9) plt.plot(utils.downsample(self.__stream_anomalies, n), color='black', alpha=0.6) plt.show()
def fsim(imageRef, imageDis): channels = imageRef.shape[1] if channels == 3: Y1 = (0.299 * imageRef[:,0,:,:] + 0.587 * imageRef[:,1,:,:] + 0.114 * imageRef[:,2,:,:]).unsqueeze(1) Y2 = (0.299 * imageDis[:,0,:,:] + 0.587 * imageDis[:,1,:,:] + 0.114 * imageDis[:,2,:,:]).unsqueeze(1) I1 = (0.596 * imageRef[:,0,:,:] - 0.274 * imageRef[:,1,:,:] - 0.322 * imageRef[:,2,:,:]).unsqueeze(1) I2 = (0.596 * imageDis[:,0,:,:] - 0.274 * imageDis[:,1,:,:] - 0.322 * imageDis[:,2,:,:]).unsqueeze(1) Q1 = (0.211 * imageRef[:,0,:,:] - 0.523 * imageRef[:,1,:,:] + 0.312 * imageRef[:,2,:,:]).unsqueeze(1) Q2 = (0.211 * imageDis[:,0,:,:] - 0.523 * imageDis[:,1,:,:] + 0.312 * imageDis[:,2,:,:]).unsqueeze(1) Y1, Y2 = downsample(Y1, Y2) I1, I2 = downsample(I1, I2) Q1, Q2 = downsample(Q1, Q2) elif channels == 1: Y1, Y2 = downsample(imageRef, imageDis) else: raise ValueError('channels error') PC1 = phasecong2(Y1) PC2 = phasecong2(Y2) dx = torch.Tensor([[3, 0, -3], [10, 0, -10], [3, 0, -3]]).float()/16 dy = torch.Tensor([[3, 10, 3], [0, 0, 0], [-3, -10, -3]]).float()/16 dx = dx.reshape(1,1,3,3).to(imageRef.device) dy = dy.reshape(1,1,3,3).to(imageRef.device) IxY1 = F.conv2d(Y1, dx, stride=1, padding =1) IyY1 = F.conv2d(Y1, dy, stride=1, padding =1) gradientMap1 = torch.sqrt(IxY1**2 + IyY1**2+1e-12) IxY2 = F.conv2d(Y2, dx, stride=1, padding =1) IyY2 = F.conv2d(Y2, dy, stride=1, padding =1) gradientMap2 = torch.sqrt(IxY2**2 + IyY2**2+1e-12) T1 = 0.85 T2 = 160 PCSimMatrix = (2 * PC1 * PC2 + T1) / (PC1**2 + PC2**2 + T1) gradientSimMatrix = (2*gradientMap1*gradientMap2 + T2)/(gradientMap1**2 + gradientMap2**2 + T2) PCm = torch.max(PC1, PC2) SimMatrix = gradientSimMatrix * PCSimMatrix * PCm FSIM_val = torch.sum(SimMatrix,dim=[1,2,3]) / torch.sum(PCm,dim=[1,2,3]) if channels==1: return FSIM_val T3 = 200 T4 = 200 ISimMatrix = (2 * I1 * I2 + T3) / (I1**2 + I2**2 + T3) QSimMatrix = (2 * Q1 * Q2 + T4) / (Q1**2 + Q2**2 + T4) SimMatrixC = gradientSimMatrix * PCSimMatrix * PCm * \ torch.sign(gradientSimMatrix) * ((torch.abs(ISimMatrix * QSimMatrix)+1e-12) ** 0.03) return torch.sum(SimMatrixC,dim=[1,2,3]) / torch.sum(PCm,dim=[1,2,3])
def __init__(self, ): super(Generator_, self).__init__() self.OUTPUT_CHANNELS = 3 self.downsample = [ downsample(64, 4, apply_batchnorm=False), downsample(128, 4), # (bs, 64, 64, 128) downsample(256, 4), # (bs, 32, 32, 256) downsample(512, 4), # (bs, 16, 16, 512) downsample(512, 4), # (bs, 8, 8, 512) downsample(512, 4), # (bs, 4, 4, 512) downsample(512, 4), # (bs, 2, 2, 512) downsample(512, 4) ] # (bs, 1, 1, 512) self.upsample = [ upsample(512, 4, apply_dropout=True), # (bs,2,2,1024) upsample(512, 4, apply_dropout=True), # (bs,4,4,1024) upsample(512, 4, apply_dropout=True), # (bs,8,8,1024) upsample(512, 4), # (bs, 16, 16, 1024) upsample(256, 4), # (bs, 32, 32, 512) upsample(128, 4), # (bs, 64, 64, 256) upsample(64, 4) # (bs, 128, 128, 128) ] initializer = tf.random_normal_initializer(0., 0.02) self.last = tf.keras.layers.Conv2DTranspose( self.OUTPUT_CHANNELS, 4, strides=2, padding='same', kernel_initializer=initializer, activation='tanh') # (bs, 256, 256, 3) self.concat = tf.keras.layers.Concatenate()
def make_lib(R=1000, wmin=1e3, wmax=1e4, velocity=True, dirname='./', name='ckc14_new', **extras): """Make a new downsampled CKC library, with the desired resolution in the desired wavelength range. This makes both binary files (one for each metallicity) and a wavelength file, suitable for use in FSPS. It also makes an hdf5 file. This is deprecated in favor of make_lib_byz below """ downsample = utils.read_and_downsample_spectra if not os.path.exists(dirname): os.makedirs(dirname) outwave, outres = utils.construct_outwave(R, wmin, wmax, velocity=velocity) wave = np.concatenate(outwave) spectra = downsample(outwave, outres, velocity=velocity, write_binary=True, binout=dirname + '{0}_z{{0}}.spectra.bin'.format(name)) with open(dirname + '{0}.lambda'.format(name), 'w') as f: for w in np.concatenate(outwave): f.write('{0:15.4f}\n'.format(w)) params = utils.spec_params(expanded=True) with h5py.File(dirname + '{0}.h5'.format(name), 'x') as f: dspec = f.create_dataset("spectra", data=spectra) dwave = f.create_dataset("wavelengths", data=wave) dpar = f.create_dataset("parameters", data=params)
def make_lib(R=1000, wmin=1e3, wmax=1e4, velocity=True, dirname='./', name='ckc14_new', **extras): """Make a new downsampled CKC library, with the desired resolution in the desired wavelength range. This makes both binary files (one for each metallicity) and a wavelength file, suitable for use in FSPS. It also makes an hdf5 file. This is deprecated in favor of make_lib_byz below """ downsample = utils.read_and_downsample_spectra if not os.path.exists(dirname): os.makedirs(dirname) outwave, outres = utils.construct_outwave(R, wmin, wmax, velocity=velocity) wave = np.concatenate(outwave) spectra = downsample(outwave, outres, velocity=velocity, write_binary=True, binout=dirname+'{0}_z{{0}}.spectra.bin'.format(name)) with open(dirname+'{0}.lambda'.format(name), 'w') as f: for w in np.concatenate(outwave): f.write('{0:15.4f}\n'.format(w)) params = utils.spec_params(expanded=True) with h5py.File(dirname + '{0}.h5'.format(name), 'x') as f: dspec = f.create_dataset("spectra", data=spectra) dwave = f.create_dataset("wavelengths", data=wave) dpar = f.create_dataset("parameters", data=params)
def gen_F(self, feature_size, num_layers, scale, y): image_input, mean_x = self.preprossessing(y, self.img_size) # One convolution before res blocks and to convert to required feature # depth # conv # input x2 ( 64*64 ) output ( 64*64 ) 64 y = slim.conv2d(image_input, feature_size, [3, 3]) # Store the output of the first convolution to add later ********* conv_1 = y scaling_factor = 0.1 # Add the residual blocks to the model for i in range(num_layers): # 32 conv_1---conv_64 y = utils.resBlock(y, feature_size, scale=scaling_factor) # One more convolution, and then we add the output of our first conv # layer ******************* # conv_65 # HR -> LR y = slim.conv2d(y, feature_size, [3, 3]) y += conv_1 # 补齐残差 # Downsample output of the convolution y = utils.downsample(y, scale, feature_size, None) # conv_66 conv_67 scale = 2 y = tf.clip_by_value(y + mean_x, 0.0, 255.0) return y
def __init__(self, path, name): self.path = path self.name = name # self.images_lr, self.names = self.load_images_by_model(model='LR') self.images_hr, self.names = self.load_images_by_model(model='HR') self.images_lr = [] for img in self.images_hr: self.images_lr.append(downsample(img, 4))
def make_lib_flatfull(R=1000, wmin=1e3, wmax=1e4, velocity=True, h5name='data/h5/ckc14_fsps.flat.h5', outfile='ckc14_new.flat.h5', **extras): """Make a new downsampled CKC library, with the desired resolution in the desired wavelength range. This makes an hdf5 file with the downsampled spectra. :param R: Desired resolution in the interval (wmin, wmax) *not including the native CKC resolution*, in terms of lambda/sigma_lambda. Ouside this interval the resolution will be 100. :param h5name: Full path to the *flat* version of the HDF file containing the full CKC grid. :param outfile: Full path to the output h5 filename. Note that this will be a *flat* spectral file, suitable for use with StarBasis() """ h5fullflat = h5name from utils import downsample_onespec as downsample # Get the output wavelength grid as segments outwave, outres = utils.construct_outwave(R, wmin, wmax, velocity=velocity) wave = np.concatenate(outwave) with h5py.File(h5fullflat, "r") as full: # Full wavelength vector and number of spectra fwave = np.array(full["wavelengths"]) nspec = full["parameters"].shape[0] with h5py.File(outfile, "w") as newf: # store the output wavelength vector newf.create_dataset("wavelengths", data=wave) # copy the spectral parameters over full.copy("parameters", newf) # create an array to hold the new spectra. news = newf.create_dataset("spectra", data=np.ones([nspec, len(wave)]) * 1e-33) # loop over the old spectra for i in xrange(nspec): s = np.array(full["spectra"][i, :]) # monitor progress if np.mod(i , np.int(nspec/10)) == 0: print("doing {0} of {1} spectra".format(i, nspec)) # don't convolve empty spectra if s.max() < 1e-32: continue # Actually do the convolution lores = downsample(fwave, s, outwave, outres, velocity=velocity) news[i, :] = np.concatenate(lores) # flush to disk so you can read the file and monitor # progress in another python instance, and if # something dies you don't totally lose the data if np.mod(i , np.int(nspec/10)) == 0: newf.flush()
def save_prediction(arg): (img_path, img_scale), pred_img = arg if not downsampled: pred_img = np.stack([utils.downsample(p, PRED_SCALE) for p in pred_img]) binarized = (pred_img * 1000).astype(np.uint16) with gzip.open( str(out_path / '{}-{:.5f}-pred.npy'.format( img_path.stem, img_scale)), 'wb', compresslevel=4) as f: np.save(f, binarized) return img_path.stem
def __init__(self, ): super(Discriminator_, self).__init__() """Define the layers used in the network.""" initializer = tf.random_normal_initializer(0., 0.02) self.down1 = downsample(64, 4, False) self.down2 = downsample(128, 4) self.down3 = downsample(256, 4) self.zero_pad1 = tf.keras.layers.ZeroPadding2D() self.conv = tf.keras.layers.Conv2D(512, 4, strides=1, kernel_initializer=initializer, use_bias=False) self.batchnorm1 = tf.keras.layers.BatchNormalization() self.leaky_relu = tf.keras.layers.LeakyReLU() self.zero_pad2 = tf.keras.layers.ZeroPadding2D() self.last = tf.keras.layers.Conv2D(1, 4, strides=1, kernel_initializer=initializer)
def Encode_MS(val_F1, val_P1, scales): ref = {} for sc in scales: if sc != 1.0: msv_F1, msv_P1 = downsample([val_F1, val_P1], sc) msv_F1, msv_P1 = ToCudaVariable([msv_F1, msv_P1], volatile=True) ref[sc] = model.module.Encoder(msv_F1, msv_P1)[0] else: msv_F1, msv_P1 = ToCudaVariable([val_F1, val_P1], volatile=True) ref[sc] = model.module.Encoder(msv_F1, msv_P1)[0] return ref
def forward(self, x, noise_sigma): noise_map = noise_sigma.view(x.shape[0], 1, 1, 1).repeat(1, x.shape[1], x.shape[2] // 2, x.shape[3] // 2) x_up = utils.downsample(x.data) # 4 * C * H/2 * W/2 x_cat = torch.cat((noise_map.data, x_up), 1) # 4 * (C + 1) * H/2 * W/2 x_cat = Variable(x_cat) h_dncnn = self.intermediate_dncnn(x_cat) y_pred = utils.upsample(h_dncnn) return y_pred
def __init__(self, path, name): self.path = path self.name = name self.images_hr, self.names = self.load_images_by_model(model='HR') self.images_lr = [] self.image_lr_edge = [] for img in self.images_hr: self.images_lr.append(downsample(img, 3)) for img in self.images_lr: self.image_lr_edge.append(cany_oper(img))
def make_lib_byz(R=1000, wmin=1e3, wmax=1e4, velocity=True, dirname='./', name='ckc14_new', **extras): """Make a new downsampled CKC library, with the desired resolution in the desired wavelength range. This makes both binary files (one for each metallicity) and a wavelength file, suitable for use in FSPS. It also makes an hdf5 file with the downsampled spectra. """ downsample = utils.read_and_downsample_onez # set up output names, directories, and files if not os.path.exists(dirname): os.makedirs(dirname) binout = dirname + '{0}_z{{0}}.spectra.bin'.format(name) wout = dirname + '{0}.lambda'.format(name) with h5py.File(dirname + '{0}.h5'.format(name), 'x') as f: spgr = f.create_group("spectra") # Get the output wavelength grid, write to hdf5 and a file outwave, outres = utils.construct_outwave(R, wmin, wmax, velocity=velocity) wave = np.concatenate(outwave) dwave = f.create_dataset("wavelengths", data=wave) with open(wout, 'w') as wf: for w in np.concatenate(outwave): wf.write('{0:15.4f}\n'.format(w)) # Parameters params = utils.spec_params(expanded=True) dpar = f.create_dataset("parameters", data=params) zlegend, logg, logt = utils.spec_params() # Get the spectra for each z binary file zlist = ['{0:06.4f}'.format(z) for z in zlegend] for z in zlist: print('doing z={}'.format(z)) zspec = downsample(z, outwave, outres, binout=binout, write_binary=True, velocity=velocity) zd = spgr.create_dataset('z{0}'.format(z), data=zspec) f.flush()
def crn(conv5, weights_file): # Contextual Reweighting Network conv5 = downsample(conv5) # g Multiscale Context Filters, dimension is Bx13x13x84 convg3x3 = conv(conv5, 3, 3, 32, 1, 1, name='convg3x3', trainable=True, initializer=tf.contrib.layers.xavier_initializer()) convg5x5 = conv(conv5, 5, 5, 32, 1, 1, name='convg5x5', trainable=True, initializer=tf.contrib.layers.xavier_initializer()) convg7x7 = conv(conv5, 7, 7, 20, 1, 1, name='convg7x7', trainable=True, initializer=tf.contrib.layers.xavier_initializer()) # convg = tf.concat([convg3x3, convg5x5, convg7x7], 3) convg = tf.concat(3, [convg3x3, convg5x5, convg7x7]) # w Accumulation Weight, 13x13x84 to 13x13x1 convw = conv(convg, 1, 1, 1, 1, 1, name='convw', trainable=True, initializer=tf.contrib.layers.xavier_initializer()) # Bx13x13x1 to BxWxHx1 m = upsample(convw) return m
def get_inner_geo(key, value_lst, idx): if key == 'highway': primlst = ['primary', 'trunk'] fpath = 'road' elif key == 'railway': primlst = ['rail'] fpath = 'rail' elif key == 'waterway': primlst = ['river'] fpath = 'river' b_path = get_boundary_path(idx) geo_line = geojson.load( codecs.open(os.path.join(root_path, 'data', 'ex_osm_line.geojson'), 'r', 'latin-1')) inner_features = [] for feature in geo_line['features']: if feature['properties'][key] in value_lst: # if feature['properties'][key] not in ['subway','platform',None]: coord = feature['geometry']['coordinates'] for i in range(len(coord) - 1): p = [coord[i], coord[i + 1]] c = [Path.MOVETO, Path.CLOSEPOLY] pth = Path(p, c) if b_path.contains_path(pth): # inner_features.append(utils.downsample(p,0.003)) if feature['properties'][key] in primlst: inner_features.append(utils.downsample(p, 0.003)) # inner_features.append(p) else: inner_features.append(p) # geo_new = {'type': 'FeatureCollection', 'features': [{'geometry':{'type':'MultiLineString','coordinates':inner_features}}]} final_features = [] for feature in inner_features: final_features.append( {'geometry': { 'type': 'LineString', 'coordinates': feature }}) geo_new = {'type': 'FeatureCollection', 'features': final_features} json.dump( geo_new, open( os.path.join(root_path, 'data', 'source', 'administrative', fpath, ADMIN_DICT[idx] + '.geojson'), 'w'))
def make_lib_byz(R=1000, wmin=1e3, wmax=1e4, velocity=True, dirname='./', name='ckc14_new', **extras): """Make a new downsampled CKC library, with the desired resolution in the desired wavelength range. This makes both binary files (one for each metallicity) and a wavelength file, suitable for use in FSPS. It also makes an hdf5 file with the downsampled spectra. """ downsample = utils.read_and_downsample_onez # set up output names, directories, and files if not os.path.exists(dirname): os.makedirs(dirname) binout = dirname+'{0}_z{{0}}.spectra.bin'.format(name) wout = dirname+'{0}.lambda'.format(name) with h5py.File(dirname + '{0}.h5'.format(name), 'x') as f: spgr = f.create_group("spectra") # Get the output wavelength grid, write to hdf5 and a file outwave, outres = utils.construct_outwave(R, wmin, wmax, velocity=velocity) wave = np.concatenate(outwave) dwave = f.create_dataset("wavelengths", data=wave) with open(wout, 'w') as wf: for w in np.concatenate(outwave): wf.write('{0:15.4f}\n'.format(w)) # Parameters params = utils.spec_params(expanded=True) dpar = f.create_dataset("parameters", data=params) zlegend, logg, logt = utils.spec_params() # Get the spectra for each z binary file zlist = ['{0:06.4f}'.format(z) for z in zlegend] for z in zlist: print('doing z={}'.format(z)) zspec = downsample(z, outwave, outres, binout=binout, write_binary=True, velocity=velocity) zd = spgr.create_dataset('z{0}'.format(z), data=zspec) f.flush()
def Propagate_MS(ref, val_F2, val_P2, scales): h, w = val_F2.size()[2], val_F2.size()[3] msv_E2 = {} for sc in scales: if sc != 1.0: msv_F2, msv_P2 = downsample([val_F2, val_P2], sc) msv_F2, msv_P2 = ToCudaVariable([msv_F2, msv_P2], volatile=True) r5, r4, r3, r2 = model.module.Encoder(msv_F2, msv_P2) e2 = model.module.Decoder(r5, ref[sc], r4, r3, r2) msv_E2[sc] = upsample( F.softmax(e2[0], dim=1)[:, 1].data.cpu(), (h, w)) else: msv_F2, msv_P2 = ToCudaVariable([val_F2, val_P2], volatile=True) r5, r4, r3, r2 = model.module.Encoder(msv_F2, msv_P2) e2 = model.module.Decoder(r5, ref[sc], r4, r3, r2) msv_E2[sc] = F.softmax(e2[0], dim=1)[:, 1].data.cpu() val_E2 = torch.zeros(val_P2.size()) for sc in scales: val_E2 += msv_E2[sc] val_E2 /= len(scales) return val_E2
def get_model_unet(): """ This endpoint return first model prediction car MASKING Input: -User image Output - Return a first model prediction images """ image_b64 = request.args.get('data') # print("--->", "AAAAA" , file=sys.stderr) # 1.- convert base64 uri o png image and save to tmp folder image_name = convert_and_save4(image_b64) # 2.- Load image to numpy array img = imread(image_name) debug_np_to_file(img, '/tmp/original.jpg') # 3.- resize image to model specs 256x256 resized_image = np.uint8(downsample(img, H, W)) debug_np_to_file(resized_image, '/tmp/resized.jpg') # 4.- convert to gray scale as model input expected gray_image = np.uint8(to_gray_scale_image(resized_image, H, W)) print("GRAY-->", np.max(gray_image), file=sys.stderr) debug_np_to_file(gray_image.reshape(H, W), '/tmp/gray.jpg') # 4.- Predict model gray_image = gray_image.reshape(H, W, 1) # adebug need to add to elements to avoid unkonwed isssue for now... lista = [] lista.append(gray_image / 255) lista.append(gray_image / 255) # model_file = 'app/input_models/unet-carvana-augmented.hdf5' # first_unet_model=load_model(model_file) with graph.as_default(): ret_img = first_unet_model.predict(np.array(lista).reshape(2, H, W, 1)) # 5 .- convert to base64 image and return endimage = np.uint8((ret_img[0].reshape(H, W) > 0.8) * 255) debug_np_to_file(endimage, '/tmp/end.jpg') ret = save_np_array_to_image(endimage) print("CONVERT!!-->", ret[:80], file=sys.stderr) return ret
def JPEG_compression(image, quality = 50,percent= 1): """ Takes in an RGB image and applys the JPEG compression algorithm Steps: -Preprocessing -DCT -Quantinization Input: quality- determines the amount of lossy compression Output: Numpy array of 8x8 blocks for each channel [number of blocks, 8,8,3] """ #Prepocessing #DownSampling im_d = utils.downsample(image, percent) #Pading to make 8x8 blocks im_pad = reflect_pad_image(im_d) #YCbCr im_yc = rgb2lab(im_pad) #Offset im_yc[:,:,[1,2]] += 128 #Blocked into 8x8 blocks and apply DCT im_dct = dct_all(im_yc) #Quantize im_q = quantize(im_dct,quality) im_vec = zigzag_blocks(im_q) #Add Meta Data im_vec = np.append(im_vec,(im_d.shape[1],im_d.shape[0],quality,image.shape[1],image.shape[0])) return im_vec.astype(np.int32)
def forward(self, x, y, as_loss=True, resize=True): assert x.shape == y.shape if resize: x, y = downsample(x, y) if as_loss: feats0 = self.forward_once(x) feats1 = self.forward_once(y) else: with torch.no_grad(): feats0 = self.forward_once(x) feats1 = self.forward_once(y) dist1 = 0 dist2 = 0 c1 = 1e-6 c2 = 1e-6 w_sum = self.alpha.sum() + self.beta.sum() alpha = torch.split(self.alpha / w_sum, self.chns, dim=1) beta = torch.split(self.beta / w_sum, self.chns, dim=1) for k in range(len(self.chns)): x_mean = feats0[k].mean([2, 3], keepdim=True) y_mean = feats1[k].mean([2, 3], keepdim=True) S1 = (2 * x_mean * y_mean + c1) / (x_mean**2 + y_mean**2 + c1) dist1 = dist1 + (alpha[k] * S1).sum(1, keepdim=True) x_var = ((feats0[k] - x_mean)**2).mean([2, 3], keepdim=True) y_var = ((feats1[k] - y_mean)**2).mean([2, 3], keepdim=True) xy_cov = (feats0[k] * feats1[k]).mean( [2, 3], keepdim=True) - x_mean * y_mean S2 = (2 * xy_cov + c2) / (x_var + y_var + c2) dist2 = dist2 + (beta[k] * S2).sum(1, keepdim=True) score = 1 - (dist1 + dist2).squeeze() if as_loss: return score.mean() else: return score
def output(self): """ Generate output of this layer """ self.x_imgs = self.prev_layer.output() return numpy.asarray(map(lambda x: downsample(x), self.x_imgs))
logger.info("\nStaring training process\n") config = parse_arguments() dataset = load_data_from_config(config['LOADING']) data_preprocessed = preprocess_data( dataset, rm_stopwords=config['PREPROCESS'].getboolean('rm_stopwords', True), stemming=config['PREPROCESS'].getboolean('stem', True)) data_doc2vec = DocumentsTagged(data_preprocessed) gen_data_doc2vec = Generator(data_doc2vec) corpus = load_and_process(gen_data_doc2vec) if config['TRAIN'].getboolean('downsample'): corpus = downsample(corpus) models_to_train = ModelsLoader.load_models_from_config( config['LOADING'], config['PARAMETERS']) models = ModelsTrainer.init_models(models_to_train, corpus) ModelsTrainer.train_from_config(models, corpus, config['TRAIN']) q_checker = QualityChecker(models, corpus) q_checker.base_check_from_config(config['TRAIN'])
############################################################################### all_predictions1 = [] for image in tqdm( torch.utils.data.DataLoader(salt_ID_dataset_test, batch_size=30, drop_last=False, num_workers=8, pin_memory=True)): image = image[0].type(torch.FloatTensor).cuda() y_pred = utils.sigmoid(model(Variable(image)).cpu().data.numpy()) all_predictions1.append(y_pred) all_predictions_stacked1 = np.vstack(all_predictions1)[:, 0, :, :] preds_valid1 = all_predictions_stacked1.reshape(-1, 128, 128) preds_valid1 = np.array([utils.downsample(x) for x in preds_valid1]) preds_valid = preds_valid1 del preds_valid1 ############################################################################### all_predictions1_flip = [] for image in tqdm( torch.utils.data.DataLoader(salt_ID_dataset_test_flip, batch_size=30, drop_last=False, num_workers=8, pin_memory=True)): image = image[0].type(torch.FloatTensor).cuda() y_pred = utils.sigmoid(model(Variable(image)).cpu().data.numpy()) all_predictions1_flip.append(y_pred) all_predictions_stacked1_flip = np.vstack(all_predictions1_flip)[:, 0, :, :]
def make_lib_flatfull(R=1000, wmin=1e3, wmax=1e4, velocity=True, h5name='fullres/fsps/ckc14/h5/ckc14_fsps.flat.h5', outfile='ckc14_new.flat.h5', **extras): """Make a new downsampled CKC library, with the desired resolution in the desired wavelength range. This makes an hdf5 file with the downsampled spectra. :param R: Desired resolution in the interval (wmin, wmax) *not including the native CKC resolution*, in terms of lambda/sigma_lambda. Ouside this interval the resolution will be 100. :param h5name: Full path to the *flat* version of the HDF file containing the full CKC grid. :param outfile: Full path to the output h5 filename. Note that this will be a *flat* spectral file, suitable for use with StarBasis() """ h5fullflat = h5name from utils import downsample_onespec as downsample # Get the output wavelength grid as segments outwave, outres = utils.construct_outwave(R, wmin, wmax, velocity=velocity) wave = np.concatenate(outwave) with h5py.File(h5fullflat, "r") as full: # Full wavelength vector and number of spectra fwave = np.array(full["wavelengths"]) nspec = full["parameters"].shape[0] with h5py.File(outfile, "w") as newf: # store the output wavelength vector newf.create_dataset("wavelengths", data=wave) # copy the spectral parameters over full.copy("parameters", newf) # create an array to hold the new spectra. news = newf.create_dataset("spectra", data=np.ones([nspec, len(wave)]) * 1e-33) # loop over the old spectra for i in xrange(nspec): s = np.array(full["spectra"][i, :]) # monitor progress if np.mod(i, np.int(nspec / 10)) == 0: print("doing {0} of {1} spectra".format(i, nspec)) # don't convolve empty spectra if s.max() < 1e-32: continue # Actually do the convolution lores = downsample(fwave, s, outwave, outres, velocity=velocity) news[i, :] = np.concatenate(lores) # flush to disk so you can read the file and monitor # progress in another python instance, and if # something dies you don't totally lose the data if np.mod(i, np.int(nspec / 10)) == 0: newf.flush()
def run_style_transfer(content_path, style_path, content_weight, max_scale, content_regions, style_regions, output_path='./output.png', print_freq=100, use_sinkhorn=False, sinkhorn_reg=0.1, sinkhorn_maxiter=30): smallest_size = 64 start = time.time() content_image, style_image = utils.load_img(content_path), utils.load_img( style_path) _, content_H, content_W = content_image.size() _, style_H, style_W = style_image.size() print( f'content image size {content_H}x{content_W}, style image size {style_H}x{style_W}' ) for scale in range(1, max_scale + 1): t0 = time.time() scaled_size = smallest_size * (2**(scale - 1)) print('Processing scale {}/{}, size {}...'.format( scale, max_scale, scaled_size)) content_scaled_size = (int(content_H * scaled_size / content_W), scaled_size) if content_H < content_W else ( scaled_size, int(content_W * scaled_size / content_H)) content_image_scaled = utils.resize(content_image.unsqueeze(0), content_scaled_size).to(device) bottom_laplacian = content_image_scaled - utils.resize( utils.downsample(content_image_scaled), content_scaled_size) lr = 2e-3 if scale == 1: style_image_mean = style_image.unsqueeze(0).mean( dim=(2, 3), keepdim=True).to(device) stylized_im = style_image_mean + bottom_laplacian elif scale > 1 and scale < max_scale: stylized_im = utils.resize(stylized_im.clone(), content_scaled_size) + bottom_laplacian elif scale == max_scale: stylized_im = utils.resize(stylized_im.clone(), content_scaled_size) lr = 1e-3 stylized_im, final_loss = style_transfer( stylized_im, content_image_scaled, style_path, output_path, scaled_size, content_weight, content_regions, style_regions, lr, print_freq=print_freq, use_sinkhorn=use_sinkhorn, sinkhorn_reg=sinkhorn_reg, sinkhorn_maxiter=sinkhorn_maxiter) content_weight /= 2.0 print('...done in {:.1f} sec, final loss {:.4f}'.format( time.time() - t0, final_loss.item())) print('Finished in {:.1f} secs'.format(time.time() - start)) canvas = torch.clamp(stylized_im[0], -0.5, 0.5).data.cpu().numpy().transpose(1, 2, 0) print(f'Saving to output to {output_path}.') imageio.imwrite(output_path, canvas) return final_loss, stylized_im