def imgapi_example(run_id, snapshot): net = imgapi_load_net(run_id, snapshot, load_dataset=False) images = net.gen_fn(net.example_latents[:1], net.example_labels[:1]) # latents: [minibatch, component], normalized automatically by the network, value represents a point on the unit hypersphere # labels: [minibatch, component], value depends on the dataset and training config # images: [minibatch, channel, height, width], dynamic range 0--255 misc.save_image(images[0], os.path.join(config.result_dir, 'debug.png'), drange=[0,255])
def __call__(self, w): if self.iter > 0: k = self.iter self.ex = 1 / k * ((k - 1) * self.ex + w[0]) self.ey = 1 / k * ((k - 1) * self.ey + w[1]) if self.iter in self.iter_save: obj = obj_fun(w[0]) breg_x = bregman_g(w[0]) breg_y = bregman_f(w[1]) breg = breg_x + breg_y breg_ex = bregman_g(self.ex) breg_ey = bregman_f(self.ey) breg_erg = breg_ex + breg_ey dx = dist_x(w[0]) dy = dist_y(w[1]) dist = dx + dy dex = dist_x(self.ex) dey = dist_y(self.ey) dist_erg = dex + dey self.out.append({'obj': obj, 'breg': breg, 'breg_x': breg_x, 'breg_y': breg_y, 'breg_erg': breg_erg, 'breg_ex': breg_ex, 'breg_ey': breg_ey, 'dist': dist, 'dist_x': dx, 'dist_y': dy, 'dist_erg': dist_erg, 'dist_ex': dex, 'dist_ey': dey}) if self.iter in self.iter_plot: fname = '{}_{}'.format(self.alg, self.iter) misc.save_image(w[0], fname, folder_today, 1, clim=clim) self.iter += 1
def update_client(self, uid=1, account_no=1, banking_id=1, service_account="", branch="", surname="", middle_name="", first_name="", contact="", occupation="", location="", rent="", spouse_parent="", spouse_contact="", dob="", age=1, gender="", address="", select_group="", family_size=1, id_card="", id_number=1, id_end_date="", place_of_worship="", next_of_kin="", nok_dob="", relative="", relative_contact="", house_no="", image="") -> int: # Insert into clients db # returns the last inserted id # Save image locally image_name = misc.save_image(image, self.media_folder) conn = sqlite3.connect(self.clients_db) cursor = conn.cursor() sql = f'account_no={account_no}, banking_id={banking_id}, service_account = "{service_account}",' sql += f'branch = "{branch}", surname="{surname}", middle_name="{middle_name}",' sql += f'first_name="{first_name}", contact="{contact}", occupation="{occupation}",' sql += f'location="{location}", rent="{rent}", spouse_parent="{spouse_parent}",' sql += f'spouse_contact="{spouse_contact}", dob="{dob}", age={age}, gender="{gender}",' sql += f'address="{address}", select_group="{select_group}", family_size={family_size}, id_card="{id_card}",' sql += f'id_number={id_number}, id_end_date="{id_end_date}", place_of_worship="{place_of_worship}",' sql += f'next_of_kin="{next_of_kin}", nok_dob="{nok_dob}", relative="{relative}", relative_contact="{relative_contact}",' sql += f'house_no="{house_no}", image="{image}"' sql = f"UPDATE clients SET {sql} WHERE uid={uid}" print('sql: ', sql) cursor.execute(sql) conn.commit() last_id = cursor.lastrowid conn.close() return last_id
def __call__(self, x, **kwargs): if self.iter in self.iter_save: obj = obj_fun(x[0]) dx = dist_x(x[0]) dy = dist_y(x[1]) d = dx + dy self.out.append({ 'obj': obj, 'dist': d, 'dist_x': dx, 'dist_y': dy }) if self.iter in self.iter_plot: fname = '{}_{}'.format(self.alg, self.iter) misc.save_image(x[0], fname, folder_today, 1, clim=clim) self.iter += 1
def __call__(self, w): if self.iter in self.iter_save: obj = obj_fun(w[0]) psnr = fom.psnr(w[0], groundtruth) psnr_opt = fom.psnr(w[0], x_opt) dx = dist_x(w[0]) dy = dist_y(w[1]) dist = dx + dy self.out.append({ 'obj': obj, 'dist': dist, 'dist_x': dx, 'dist_y': dy, 'psnr': psnr, 'psnr_opt': psnr_opt }) if self.iter in self.iter_plot: fname = '{}_{}'.format(self.alg, self.iter) misc.save_image(w[0], fname, folder_today, 1, clim=clim) self.iter += 1
kernel = images.blurring_kernel(shape=[15, 15]) convolution = misc.Blur2D(X, kernel) K = odl.uniform_discr([0, 0], kernel.shape, kernel.shape) kernel = K.element(kernel) scale = 1e+3 A = odl.BroadcastOperator(Dx, Dy, scale / clim[1] * convolution) Y = A.range # create data background = 200 * Y[2].one() data = odl.phantom.poisson_noise(A[2](groundtruth) + background, seed=1807) # save images and data if not os.path.exists('{}/groundtruth.png'.format(folder_main)): misc.save_image(groundtruth, 'groundtruth', folder_main, 1, clim=clim) misc.save_image(data - background, 'data', folder_main, 2, clim=[0, scale]) misc.save_image(kernel, 'kernel', folder_main, 3) alpha = 0.1 # set regularisation parameter gamma = 0.99 # auxiliary step size parameter < 1 # set up functional f f = odl.solvers.SeparableSum( odl.solvers.Huber(A[0].range, gamma=1), odl.solvers.Huber(A[1].range, gamma=1), 1 / alpha * misc.KullbackLeiblerSmooth(A[2].range, data, background)) g = odl.solvers.IndicatorBox(X, clim[0], clim[1]) # set up functional g obj_fun = f * A + g # define objective function
def save_image(x, n, f): misc.save_image(x.asarray(), n, f, planes=planes, clim=clim) xs = smoothing(x) n = 'smoothed_{}'.format(n) misc.save_image(xs.asarray(), n, f, planes=planes, clim=clim)
def smoothing(x): return X.element(gaussian_filter(x.asarray(), sigma=sd_smoothing)) def save_image(x, n, f): misc.save_image(x.asarray(), n, f, planes=planes, clim=clim) xs = smoothing(x) n = 'smoothed_{}'.format(n) misc.save_image(xs.asarray(), n, f, planes=planes, clim=clim) if not os.path.exists('{}/pics/gray_image_pet.png'.format(folder_main)): tmp = X.element() fldr = '{}/pics'.format(folder_main) K.toodl(image, tmp) misc.save_image(tmp.asarray(), 'image_pet', fldr, planes=planes) K.toodl(image_mr, tmp) misc.save_image(tmp.asarray(), 'image_mr', fldr, planes=planes) K.toodl(image_ct, tmp) misc.save_image(tmp.asarray(), 'image_ct', fldr, planes=planes) # %% --- get target --- BE CAREFUL, THIS TAKES TIME file_target = '{}/target.npy'.format(folder_main) if not os.path.exists(file_target): print('file {} does not exist. Compute it.'.format(file_target)) x_opt = X.one() misc.MLEM(x_opt, KL.data, KL.background, K, nepoch_target,
def save_image(x, n, f): misc.save_image(x.asarray(), n, f, planes=planes, clim=clim)
def get_concat_h(im1, im2): dst = PIL.Image.new('L', (im1.width + im2.width, im1.height)) dst.paste(im1, (0, 0)) dst.paste(im2, (im1.width, 0)) return dst def get_concat_v(im1, im2): dst = PIL.Image.new('L', (im1.width, im1.height + im2.height)) dst.paste(im1, (0, 0)) dst.paste(im2, (0, im1.height)) return dst #tiles_left = cv2.vconcat([tiles[0], tiles[2]]) #tiles_right = cv2.vconcat([tiles[1], reshaped_im) #final_im = cv2.hconcat([tiles_left, tiles_right]) #cv2.imwrite('image_big.png', final_im) #right_org = get_concat_v(tiles_lt, tiles_lb) #left_org = get_concat_v(tiles_rt, reshaped_im) #get_concat_h(right_org, left_org).save('large_image.png') #misc.save_image_grid(images, 'img.png', drange=drange_net, grid_size= (15,8)) misc.save_image(gen_images[0], '/root/UNOSAT/valresults/' + filenames[count], drange=drange_net, quality=95) count += 1 #PIL.Image.fromarray(images[0]).save('img.png') #for idx in range(images.shape[0]): # PIL.Image.fromarray(images[idx]).save('img.png')
def get_2d_slerp_points(num_points, plane): points = [] for i in np.arange(0.0, 1.0, 1.0 / math.sqrt(num_points)): x = slerp(i, plane[0], plane[1]) for j in np.arange(0.0, 1.0, 1.0 / math.sqrt(num_points)): y = slerp(j, x, plane[2]) points = np.concatenate((points, y), axis=0) return points.reshape((num_points, 512)).astype("float32") net = train.imgapi_load_net( "000-wikiart-512x512", "/home/cameron/Projects/progressive_growing_of_gans/results/000-wikiart-512x512/network-snapshot-006200.pkl", load_dataset=False, random_seed=np.int64(10000 * random.random())) for i in range(100): plane = train.random_latents(3, net.G.input_shape) grid_shape = 10 + int(random.random() * 20) points = get_2d_slerp_points(grid_shape * grid_shape, plane) images = net.gen_fn(points, net.example_labels[:512]) grid = create_image_grid(images, (grid_shape, grid_shape), padding=10) filename = os.path.join(config.result_dir, "grids", "grid%s.%s") file_num = 1 while os.path.exists(filename % (file_num, "png")): file_num += 1 misc.save_image(grid, filename % (file_num, "png"), drange=[0, 255]) np.save(filename % (file_num, "npy"), points)
c = float(norm_K) / float(norm_D) D *= c norm_D *= c L1 = (alpha / c) * odl.solvers.GroupL1Norm(D.range) L164 = (alpha / c) * odl.solvers.GroupL1Norm(D.range.astype('float64')) g = odl.solvers.IndicatorBox(X, lower=0) obj_fun = KL * K + L1 * D + g # objective functional fldr = '{}/pics'.format(folder_param) if not os.path.exists('{}/gray_image_pet.png'.format(fldr)): tmp = X.element() tmp_op = mMR.operator_mmr() tmp_op.toodl(image, tmp) misc.save_image(tmp.asarray(), 'image_pet', fldr, planes=planes) tmp_op.toodl(image_mr, tmp) misc.save_image(tmp.asarray(), 'image_mr', fldr, planes=planes) tmp_op.toodl(image_ct, tmp) misc.save_image(tmp.asarray(), 'image_ct', fldr, planes=planes) tmp = norm(sideinfo_grad) misc.save_image(tmp.asarray(), 'image_norm_sideinfo_grad', fldr, planes=planes, cmaps={'gray'}, clim=[0, 100]) tmp = norm(xi) misc.save_image(tmp.asarray(),
smoothed_support = Y.element([ gaussian_filter(sino_support, sigma=[1, 2 / X.cell_sides[0]]) for sino_support in sinogram_support ]) background = 10 * smoothed_support + 10 background *= counts_background / background.ufuncs.sum() data = odl.phantom.poisson_noise(factors * sinogram + background, seed=1807) arr = np.empty(3, dtype=object) arr[0] = data arr[1] = factors arr[2] = background np.save(file_data, arr) misc.save_image(groundtruth, 'groundtruth', folder_main, 1, clim=clim) fig2 = plt.figure(2) fig2.clf() i = 0 plt.plot((sinogram[i]).asarray()[0], label='G(x)') plt.plot((factors[i] * sinogram[i]).asarray()[0], label='factors * G(x)') plt.plot(data[i].asarray()[0], label='data') plt.plot(background[i].asarray()[0], label='background') plt.legend() fig2.savefig('{}/components1D.png'.format(folder_main), bbox_inches='tight') else: (data, factors, background) = np.load(file_data, allow_pickle=True)
def add_client(self, account_no=1, banking_id=1, service_account="", branch="", surname="", middle_name="", first_name="", contact="", occupation="", location="", rent="", spouse_parent="", spouse_contact="", dob="", age=1, gender="", address="", select_group="", family_size=1, id_card="", id_number=1, id_end_date="", place_of_worship="", next_of_kin="", nok_dob="", relative="", relative_contact="", house_no="", image="") -> int: # Insert into clients db # returns the last inserted id # Save image locally image_name = misc.save_image(image, self.media_folder) conn = sqlite3.connect(self.clients_db) cursor = conn.cursor() tables = "account_no, banking_id, service_account, " tables += "branch, surname, middle_name, " tables += "first_name, contact, occupation, " tables += "location, rent, spouse_parent, " tables += "spouse_contact, dob, age, gender, " tables += "address, select_group, family_size, id_card, " tables += "id_number, id_end_date, place_of_worship, " tables += "next_of_kin, nok_dob, relative, relative_contact, " tables += "house_no, image" value_s = "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?" value_s += "?, ?, ?, ?, ?, ?, ?, ?, ?, ?" value_s += "?, ?, ?, ?, ?, ?, ?, ?, ?" sql = f"INSERT INTO clients ({tables}) " sql += "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, " sql += "?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" values = (account_no, banking_id, service_account, branch, surname, middle_name, first_name, contact, occupation, location, rent, spouse_parent, spouse_contact, dob, age, gender, address, select_group, family_size, id_card, id_number, id_end_date, place_of_worship, next_of_kin, nok_dob, relative, relative_contact, house_no, image_name) print('values here: ', values) cursor.execute(sql, values) conn.commit() last_id = cursor.lastrowid conn.close() print(f'Insert Complete Last id is {last_id}') print('Here goes the values\n') print(values) return last_id
# set latex options matplotlib.rc('text', usetex=False) # create ground truth image_gray = images.building(gray=True) X = odl.uniform_discr([0, 0], image_gray.shape, image_gray.shape) groundtruth = X.element(image_gray) clim = [0, 1] # create data data = odl.phantom.white_noise(X, mean=groundtruth, stddev=0.1, seed=1807) # save images and data if not os.path.exists('{}/groundtruth.png'.format(folder_main)): misc.save_image(groundtruth, 'groundtruth', folder_main, 1, clim=clim) misc.save_image(data, 'data', folder_main, 2, clim=clim) alpha = .12 # set regularisation parameter gamma = 0.99 # gamma^2 is upper bound of step size constraint # create forward operators Dx = odl.PartialDerivative(X, 0, pad_mode='symmetric') Dy = odl.PartialDerivative(X, 1, pad_mode='symmetric') A = odl.BroadcastOperator(Dx, Dy) Y = A.range # set up functional f f = odl.solvers.SeparableSum(*[odl.solvers.L1Norm(Yi) for Yi in Y]) # set up functional g g = 1 / (2 * alpha) * odl.solvers.L2NormSquared(X).translated(data)
import theano import lasagne import dataset import network from theano import tensor as T import config import misc import numpy as np import scipy.ndimage _, _, G = misc.load_pkl("network-snapshot-009041.pkl") class Net: pass net = Net() net.G = G import train num_example_latents = 10 net.example_latents = train.random_latents(num_example_latents, net.G.input_shape) net.example_labels = net.example_latents net.latents_var = T.TensorType('float32', [False] * len(net.example_latents.shape))('latents_var') net.labels_var = T.TensorType('float32', [False] * len(net.example_latents.shape)) ('labels_var') print("HIYA", net.example_latents[:1].shape, net.example_labels[:1].shape) net.images_expr = net.G.eval(net.latents_var, net.labels_var, ignore_unused_inputs=True) net.images_expr = misc.adjust_dynamic_range(net.images_expr, [-1,1], [0,1]) train.imgapi_compile_gen_fn(net) images = net.gen_fn(net.example_latents[:1], net.example_labels[:1]) misc.save_image(images[0], "fake4c.png", drange=[0,1])
def save_image(x, n, f): misc.save_image(x[0].asarray(), n, f, planes=planes, clim=clim) misc.save_image(norm_vfield(x[1]).asarray(), n + '_norm_vfield', f, planes=planes)