def Smith_net(reuse=tf.AUTO_REUSE): """Fully described network. This method is basically "data as code" Returns: struct with fields: addl_padding: amount of padding needed for an input to model_fn model_fn: function that takes: """ psi = win.fst3d_psi_factory([5, 9, 9]) phi = win.fst3d_phi_window_3D([5, 9, 9]) layer_params = layerO((3, 1, 1), 'valid') def model_fn(x): """ Args: x: tf.placeholder in (height, width, nbands) format, shape must be (25,25,nbands+6) """ return hyper3d_net( x, reuse=reuse, psis=[psi, psi], phi=phi, layer_params=[layer_params, layer_params, layer_params]) return netO(model_fn, (24, 24, 12))
def custom_net(s1=9, s2=9, e1=3, e2=3): psi1 = win.fst3d_psi_factory([e1, s1, s1]) psi2 = win.fst3d_psi_factory([e2, s2, s2]) phi = win.fst3d_phi_window_3D([e2, s2, s2]) lp1 = layerO((min(e1, 5), 1, 1), 'valid') lp2 = layerO((min(e2, 5), 1, 1), 'valid') lp3 = layerO((min(e2, 5), 1, 1), 'valid') def model_fn(x): return hyper3d_net(x, reuse=False, psis=[psi1, psi2], phi=phi, layer_params=[lp1, lp2, lp3]) return netO(model_fn, (s1 + s2 + s2 - 3, s1 + s2 + s2 - 3, e1 + e2 + e2 - 3))
def PaviaR_net(reuse=tf.AUTO_REUSE): psi = win.fst3d_psi_factory([3, 9, 9]) phi = win.fst3d_phi_window_3D([3, 9, 9]) layer_params = layerO((3, 1, 1), 'valid') def model_fn(x): """ Args: x: tf.placeholder in (height, width, nbands) format, shape must be (25,25,nbands+6) """ return hyper3d_net( x, reuse=reuse, psis=[psi, psi], phi=phi, layer_params=[layer_params, layer_params, layer_params]) return netO(model_fn, (24, 24, 6))
def KSC_net(reuse=tf.AUTO_REUSE): s = 11 psi1 = win.fst3d_psi_factory([3, s, s]) psi2 = win.fst3d_psi_factory([8, s, s]) phi = win.fst3d_phi_window_3D([8, s, s]) lp1 = layerO((1, 1, 1), 'valid') lp2 = layerO((8, 1, 1), 'valid') lp3 = layerO((8, 1, 1), 'valid') def model_fn(x): """ Args: x: tf.placeholder in (height, width, nbands) format, shape must be (25,25,nbands+6) """ return hyper3d_net(x, reuse=reuse, psis=[psi1, psi2], phi=phi, layer_params=[lp1, lp2, lp3]) return netO(model_fn, ((s - 1) * 3, (s - 1) * 3, 0))
def preprocess_data(data, st_net_spec, patch_size=51): """ST preprocess the whole data cube. Pad data, then pass in as few subsets of this data. """ s = time.time() reuse = tf.AUTO_REUSE # Network info layer_params = layerO((1,1,1), 'valid') preprocessing_mode = 'ST' if st_net_spec.psi1 and st_net_spec.psi2 and st_net_spec.phi: print('Will perform Scattering...') psi1 = win.fst3d_psi_factory(st_net_spec.psi1) psi2 = win.fst3d_psi_factory(st_net_spec.psi2) psis=[psi1,psi2] phi = win.fst3d_phi_window_3D(st_net_spec.phi) net_addl_padding = net_addl_padding_from_spec(st_net_spec) layer_params=[layer_params, layer_params, layer_params] elif st_net_spec.psi1 and st_net_spec.psi2 is None and st_net_spec.phi is None: # just one layer gabor print('Will perform Gabor filtering...') psis = [win.gabor_psi_factory(st_net_spec.psi1)] b, h, w = list(tupsum(tuple(st_net_spec.psi1), (-1,-1,-1))) net_addl_padding = (h,w,b) layer_params=[layer_params] preprocessing_mode = 'Gabor' elif st_net_spec.psi1 is None and st_net_spec.psi2 is None and st_net_spec.phi is None: # tang WST # OK I am sorry for this temporary kludge, will make a new st_net_spec struct soon print('Will perform Tang-WST...') preprocessing_mode = 'Tang-WST' kernel_size = [7,7,7] max_scale = 3 K = 3 psi = win.tang_psi_factory(max_scale, K, kernel_size) psis=[psi,psi] phi = win.tang_phi_window_3D(max_scale, kernel_size) net_addl_padding = net_addl_padding_from_spec(st_net_spec_struct(kernel_size,kernel_size,kernel_size)) layer_params=[layer_params, layer_params, layer_params] else: raise ValueError('This ST spec is not supported') # END Network info [height, width, nbands] = data.shape hyper_pixel_shape = (1, 1,data.shape[2]) all_pixels = np.array(list(itertools.product(range(width),range(height)))) ap = np.array(net_addl_padding) assert np.all(ap[:2] % 2 == 0), 'Assymetric padding is not supported' padded_data = np.pad(data, ((ap[0]//2,ap[0]//2),(ap[1]//2,ap[1]//2),(ap[2]//2,ap[2]//2)), PAD_TYPE) # cover the data with patches patch_xs = [max(0,width - (x*patch_size)) for x in range(1, width // patch_size + 2)] patch_ys = [max(0,height - (y*patch_size)) for y in range(1, height // patch_size + 2)] patch_ul_corners = itertools.product(patch_xs, patch_ys) # upper left corners addl_spatial_pad = (patch_size-1, patch_size-1, 0) batch_item_shape = tupsum(hyper_pixel_shape, net_addl_padding, addl_spatial_pad) x = tf.placeholder(tf.float32, shape=batch_item_shape) print('Compiling Graph...') compile_start = time.time() if preprocessing_mode == 'ST': feat = scat3d_to_3d_nxn_2layer(x, reuse, psis, phi, layer_params, final_size=patch_size) elif preprocessing_mode == 'Gabor': feat = gabor_mag_filter(x, reuse, psis, layer_params, final_size=patch_size) elif preprocessing_mode == 'Tang-WST': feat = scat3d_to_3d_nxn_2layer(x, reuse, psis, phi, layer_params, final_size=patch_size, tang_mode=True) compile_time = time.time() - compile_start feat_shape = tuple([int(d) for d in feat.shape]) print('Graph Compiled %is. Feature dimension per pixel is now %i.' % (int(compile_time), feat_shape[2])) assert feat_shape[0] == feat_shape[1], 'ST spatial output is not square!' assert feat_shape[0] == patch_size, 'ST spatial output size is %i, expected %i!' % (feat_shape[0], patch_size) new_data = np.zeros((height,width,feat_shape[2])) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for pixel_i, pixel in enumerate(tqdm(patch_ul_corners, desc=('Performing %s: ' % preprocessing_mode), total=len(patch_xs)*len(patch_ys))): [pixel_x, pixel_y] = pixel subimg = padded_data[pixel_y:(patch_size+pixel_y+ap[0]), pixel_x:(patch_size+pixel_x+ap[1]), :] feed_dict = {x: subimg} new_data[pixel_y:(patch_size+pixel_y), pixel_x:(patch_size+pixel_x)] = sess.run(feat, feed_dict) tf.reset_default_graph() print('ST preprocessing finished in %is.' % int(time.time() - s)) return new_data