def pool(self, y): print ("pool") '''apply pooling to unpooled output''' if self.vis_shape is None: self.vis_shape = test_shape(y)[-2:] (p_y, p_inds) = pooling.maxpool2d(y, winsize=self.poolsize,stride=self.poolstride) return (p_y, p_inds)
def define_coarse_stack(self, imnet_feats): full1 = self.create_unit('full1', ninput=test_shape(imnet_feats)[1]) f_1 = relu(full1.infer(imnet_feats)) f_1_drop = random_zero(f_1, 0.5) f_1_mean = 0.5 * f_1 full2 = self.create_unit('full2', ninput=test_shape(f_1_mean)[1]) f_2_drop = full2.infer(f_1_drop) f_2_mean = full2.infer(f_1_mean) # prediction (h, w) = self.output_size pred_drop = f_2_drop.reshape((self.bsize, h, w)) pred_mean = f_2_mean.reshape((self.bsize, h, w)) self.coarse = MachinePart(locals())
def pool(self, y): print "pool" '''apply pooling to unpooled output''' if self.vis_shape is None: self.vis_shape = test_shape(y)[-2:] (p_y, p_inds) = pooling.maxpool2d(y, winsize=self.poolsize,stride=self.poolstride) return (p_y, p_inds)
def define_coarse_stack(self, imnet_feats): full1 = self.create_unit('full1', ninput=test_shape(imnet_feats)[1]) f_1 = relu(full1.infer(imnet_feats)) f_1_drop = random_zero(f_1, 0.5) f_1_mean = 0.5 * f_1 full2 = self.create_unit('full2', ninput=test_shape(f_1_mean)[1]) f_2_drop = full2.infer(f_1_drop) f_2_mean = full2.infer(f_1_mean) # prediction (h, w) = self.coarse_output_size pred_drop = f_2_drop.reshape((self.bsize, h, w)) pred_mean = f_2_mean.reshape((self.bsize, h, w)) self.coarse = MachinePart(locals())
def conv_theano_mm(x, k, border_mode, transpose=False, stride=1): #输入图片x: (bsize, xchan, h, w) #k为滤波器:(nfilt, xchan, filt_h, filt_w) (xh, xw) = test_shape(x)[-2:] (kh, kw) = test_shape(k)[-2:] if border_mode == 'valid': pad = (0, 0) elif border_mode == 'same': #卷积后的图片大小与原图片的大小相同,因此左右两边都要加上卷积核宽度的一半 pad = (kh // 2, kw // 2) elif border_mode == 'full': pad = (kh - 1, kw - 1) else: raise ValueError(border_mode) if stride != 1 and not transpose and _mm_enable_compatibility_padding: print 'True' # semi-compatibility with cudaconv # cudaconv strided convs go one filter tile past the end at the # bottom/right. Get the same size with some extra padding if needed. # The padding is centered, so this results in up to a half-stride image # shift to the right, not exactly the same as before. if border_mode != 'valid': raise NotImplementedError() old_h = np.ceil((xh - kh) / float(stride)) * stride + kh old_w = np.ceil((xw - kw) / float(stride)) * stride + kw pad = (int(np.ceil( (old_h - xh) / 2.0)), int(np.ceil((old_w - xw) / 2.0))) if transpose: (ph, pw) = pad bottom_shape = T.constant( np.array((stride * (xh - 1) - 2 * ph + kh, stride * (xw - 1) - 2 * pw + kw))) res = theano.sandbox.cuda.blas.GpuCorrMM_gradInputs( pad=pad, subsample=(stride, stride)) \ (k, x, shape=bottom_shape) else: res = theano.sandbox.cuda.blas.GpuCorrMM( pad=pad, subsample=(stride, stride)) \ (x, k) return res
def conv_theano_mm(x, k, border_mode, transpose=False, stride=1): #输入图片x: (bsize, xchan, h, w) #k为滤波器:(nfilt, xchan, filt_h, filt_w) (xh, xw) = test_shape(x)[-2:] (kh, kw) = test_shape(k)[-2:] if border_mode == 'valid': pad = (0,0) elif border_mode == 'same':#卷积后的图片大小与原图片的大小相同,因此左右两边都要加上卷积核宽度的一半 pad = (kh // 2, kw // 2) elif border_mode == 'full': pad = (kh - 1, kw - 1) else: raise ValueError(border_mode) if stride != 1 and not transpose and _mm_enable_compatibility_padding: print 'True' # semi-compatibility with cudaconv # cudaconv strided convs go one filter tile past the end at the # bottom/right. Get the same size with some extra padding if needed. # The padding is centered, so this results in up to a half-stride image # shift to the right, not exactly the same as before. if border_mode != 'valid': raise NotImplementedError() old_h = np.ceil((xh - kh) / float(stride)) * stride + kh old_w = np.ceil((xw - kw) / float(stride)) * stride + kw pad = (int(np.ceil((old_h - xh) / 2.0)), int(np.ceil((old_w - xw) / 2.0))) if transpose: (ph, pw) = pad bottom_shape = T.constant(np.array((stride * (xh - 1) - 2*ph + kh, stride * (xw - 1) - 2*pw + kw))) res = theano.sandbox.cuda.blas.GpuCorrMM_gradInputs( pad=pad, subsample=(stride, stride)) \ (k, x, shape=bottom_shape) else: res = theano.sandbox.cuda.blas.GpuCorrMM( pad=pad, subsample=(stride, stride)) \ (x, k) return res
def define_cost(self, pred, y0, m0): bsize = self.bsize npix = int(np.prod(test_shape(y0)[1:])) y0_target = y0.reshape((self.bsize, npix)) y0_mask = m0.reshape((self.bsize, npix)) pred = pred.reshape((self.bsize, npix)) p = pred * y0_mask t = y0_target * y0_mask d = (p - t) nvalid_pix = T.sum(y0_mask, axis=1) depth_cost = (T.sum(nvalid_pix * T.sum(d**2, axis=1)) - 0.5*T.sum(T.sum(d, axis=1)**2)) \ / T.maximum(T.sum(nvalid_pix**2), 1) return depth_cost