def test_correlate(self): with self.test_session(): l = np.array([1, 2, 3, 4], dtype='float32').reshape([1, 4, 1]) l_ = tf.constant(l) r = np.array([0, 1, 0], dtype='float32').reshape([3, 1, 1]) r_ = tf.constant(r) y_ = np.array([2, 3], dtype='float32').reshape([1, 2, 1]) y = util.correlate(l, r) self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5) r_ = np.array([0, 0, 1], dtype='float32').reshape([3, 1, 1]) r = tf.constant(r_) y_ = np.array([3, 4], dtype='float32').reshape([1, 2, 1]) y = util.correlate(l, r) self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def gen_image(I): global recorrelate, corr, heatmaps, map_out if recorrelate: heatmaps = np.zeros((len(filters_ind), I.shape[0], I.shape[1]), dtype=np.float32) for j, i in enumerate(filters_ind): heatmaps[j] = correlate(I, filters[i], step=2) recorrelate = False corr = np.max(heatmaps, axis=0) map_out = CMAP(corr)[:,:,:3] * 255 print(np.max(corr)) boxes = [] output = np.zeros(I.shape[:2], dtype=np.float32) for j, i in enumerate(filters_ind): mask = (heatmaps[j] > heat_thresh[i]) * corr output = np.maximum(output, mask) mask_boxes = get_boxes(mask) boxes += min_box_size(mask_boxes, filters[i].shape[0], filters[i].shape[1], I.shape[0], I.shape[1]) I = draw_boxes(I, boxes) left = np.concatenate((I, gray2rgb(output > 0) * I), 0) right = np.concatenate((gray2rgb(corr * 255).astype(np.uint8), map_out.astype(np.uint8)), 0) height_diff = right.shape[0] - compound_filter.shape[0] filter_image = np.pad(compound_filter, [(0,height_diff), (0,0), (0,0)], mode='constant') return np.concatenate((left, right, filter_image), 1)
def test_correlate_complex(self): with self.test_session(): l = np.array([1j, 2j, 3j, 4j], dtype='complex64').reshape([1, 4, 1]) l_ = tf.constant(l) r = np.array([0, 1j, 0], dtype='complex64').reshape([3, 1, 1]) r_ = tf.constant(r) y_ = np.array([-2, -3], dtype='complex64').reshape([1, 2, 1]) y = util.correlate(l, r) self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5) r_ = np.array([0, 0, 1j], dtype='complex64').reshape([3, 1, 1]) r = tf.constant(r_) y_ = np.array([-3, -4], dtype='complex64').reshape([1, 2, 1]) y = util.correlate(l, r) self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def SCFeqns(u_z, chi, chi_s, sigma, n_avg, p_i, phi_b=0, dump_phi=False): """ System of SCF equation for terminally attached polymers. Formatted for input to a nonlinear minimizer or solver. The sign convention here on u is "backwards" and always has been. It saves a few sign flips, and looks more like Cosgrove's. """ g_z = exp(u_z) # normalize g_z for numerical stability u_z_avg = np.mean(u_z) g_z_norm = g_z / exp(u_z_avg) phi_z = calc_phi_z(g_z_norm, n_avg, sigma, phi_b, u_z_avg, p_i) if dump_phi: return phi_z # # Change in entropy from reference state per unit area (S-S*)/kL (4.2.69) # s = -((1-phi_z)*log(1-phi_z)+phi_z*(log(c*n_avg)/n_avg+u_z)).sum() # print('s:',s) # # excess polymer # theta_z = phi_z - sigma*n_avg # penalize attempts that overfill the lattice toomuch = phi_z > .99999 penalty_flag = toomuch.any() if penalty_flag: penalty = np.where(toomuch, phi_z - .99999, 0) phi_z[toomuch] = .99999 # calculate new potentials u_prime = log((1.0 - phi_z) / (1.0 - phi_b)) phi_z_avg = correlate(phi_z, LAMBDA_ARRAY, 1) u_int = 2 * chi * (phi_z_avg - phi_b) u_int[0] += chi_s # # change in Free Energy (A-A*)/kTL (4.2.70) # a = -s + np.sum((1-phi_z)*chi*phi_z_avg) + chi_s # print('a:',a) # # surface tension gamma/kT (4.2.72) # # # surface tension difference from solvent (4.2.75) # dgamma = (1-1/n_avg)*(theta_z)+np.sum(-u_prime)+chi*(np.sum( # phi_z*phi_z_avg)-phi_b**2) # print('dgamma:',dgamma) u_z_new = u_prime + u_int eps_z = u_z - u_z_new if penalty_flag: np.copysign(penalty, eps_z, penalty) eps_z += penalty return eps_z
def forward(self, x): # X.SHAPE = Bx2xcxhxw x1 = torch.tensor(x[:, 0]).type( torch.cuda.FloatTensor) ## x1.shape = B,C,H,W # print("x.shape",np.shape(x1)) x2 = torch.tensor(x[:, 1]).type(torch.cuda.FloatTensor) # x3 = torch.tensor(x[:,2]).type(torch.cuda.FloatTensor) # x1 = np.rollaxis(x1,2,1) # x1 = np.rollaxis(x1,3,2) # skimage.io.imshow(x1[0]) # plt.show() out_conv1a = self.conv1(x1) out_conv2a = self.conv2(out_conv1a) out_conv3a = self.conv3(out_conv2a) out_conv1b = self.conv1(x2) out_conv2b = self.conv2(out_conv1b) out_conv3b = self.conv3(out_conv2b) out_conv_redir = self.conv_redir(out_conv3a) out_correlation = correlate(out_conv3a, out_conv3b) in_conv3_1 = torch.cat([out_conv_redir, out_correlation], dim=1) out_conv3 = self.conv3_1(in_conv3_1) out_conv4 = self.conv4_1(self.conv4(out_conv3)) out_conv5 = self.conv5_1(self.conv5(out_conv4)) out_conv6 = self.conv6_1(self.conv6(out_conv5)) flow6 = self.predict_flow6(out_conv6) flow6_up = crop_like(self.upsampled_flow6_to_5(flow6), out_conv5) out_deconv5 = crop_like(self.deconv5(out_conv6), out_conv5) concat5 = torch.cat((out_conv5, out_deconv5, flow6_up), 1) flow5 = self.predict_flow5(concat5) flow5_up = crop_like(self.upsampled_flow5_to_4(flow5), out_conv4) out_deconv4 = crop_like(self.deconv4(concat5), out_conv4) concat4 = torch.cat((out_conv4, out_deconv4, flow5_up), 1) flow4 = self.predict_flow4(concat4) flow4_up = crop_like(self.upsampled_flow4_to_3(flow4), out_conv3) out_deconv3 = crop_like(self.deconv3(concat4), out_conv3) concat3 = torch.cat((out_conv3, out_deconv3, flow4_up), 1) flow3 = self.predict_flow3(concat3) flow3_up = crop_like(self.upsampled_flow3_to_2(flow3), out_conv2a) out_deconv2 = crop_like(self.deconv2(concat3), out_conv2a) concat2 = torch.cat((out_conv2a, out_deconv2, flow3_up), 1) flow2 = self.predict_flow2(concat2) # print("mnsadkandn") # print("shape",flow2.size(),flow3.size(),flow4.size(),flow5.size(),flow6.size()) if self.training: return flow2, flow3, flow4, flow5, flow6 else: return flow2
def phi_jz_avg(phi_jz): """ Convolve the transition matrix with the density field. For now, treat ends with special cases, add j[0] to phi[0], j[J] to phi[Z] later, pass in phi_b_below_j, phi_b_above_j and add to each correlation """ types, layers = phi_jz.shape avg = np.empty_like(phi_jz) for j, phi_z in enumerate(phi_jz): avg[j] = correlate(phi_z, LAMBDA_ARRAY, 1) avg[0, 0] += LAMBDA_1 # avg[types-1,layers-1] += LAMBDA_1 return avg
def compute_convolution(I, T, stride=None): ''' This function takes an image <I> and a template <T> (both numpy arrays) and returns a heatmap where each grid represents the output produced by convolution at each location. You can add optional parameters (e.g. stride, window_size, padding) to create additional functionality. ''' (n_rows,n_cols,n_channels) = np.shape(I) ''' BEGIN YOUR CODE ''' # heatmap = np.random.random((n_rows, n_cols)) if not stride: stride = 1 heatmap = correlate(I, T, stride) ''' END YOUR CODE ''' return heatmap
def _adjoint(self, input): return util.correlate(input, self.filt_adj, mode=self.mode_adj)