def merge(tbl): inp = scn.InputBatch(2, spatial_size) center = spatial_size.float().view(1, 2) / 2 p = torch.LongTensor(2) v = torch.FloatTensor([1, 0, 0]) for char in tbl['input']: inp.addSample() for stroke in char: stroke = stroke.float() * (Scale - 0.01) / 255 - 0.5 * (Scale - 0.01) stroke += center.expand_as(stroke) ############################################################### # To avoid GIL problems use a helper function: scn.dim_fn(2, 'drawCurve')(inp.metadata.ffi, inp.features, stroke) ############################################################### # Above is equivalent to : # x1,x2,y1,y2,l=0,stroke[0][0],0,stroke[0][1],0 # for i in range(1,stroke.size(0)): # x1=x2 # y1=y2 # x2=stroke[i][0] # y2=stroke[i][1] # l=1e-10+((x2-x1)**2+(y2-y1)**2)**0.5 # v[1]=(x2-x1)/l # v[2]=(y2-y1)/l # l=max(x2-x1,y2-y1,x1-x2,y1-y2,0.9) # for j in numpy.arange(0,1,1/l): # p[0]=math.floor(x1*j+x2*(1-j)) # p[1]=math.floor(y1*j+y2*(1-j)) # inp.setLocation(p,v,False) ############################################################### inp.precomputeMetadata(precomputeStride) return {'input': inp, 'target': torch.LongTensor(tbl['target'])}
def __getitem__(self, idx): print('load ' + self.test_data_path + str(idx) + '.msg') data = msgpack.load(open(self.test_data_path + str(idx) + '.msg', 'rb')) input_nz, input_val = data[b'input_nz'], data[b'input_val'] input = scn.InputBatch(3, self.spatialSize) input.addSample() input.setLocations( torch.from_numpy(input_nz).long() + self.input_offset.view(1, 3), torch.from_numpy(input_val).view(len(input_val), 1), 0) input.precomputeMetadata(self.precomputeStride) return {'input': input}
def merge(tbl): inp = scn.InputBatch(2, spatial_size) center = spatial_size.float().view(1, 2) / 2 p = torch.LongTensor(2) v = torch.FloatTensor([1, 0, 0]) for char in tbl['input']: inp.addSample() m = torch.eye(2) r = random.randint(1, 3) alpha = random.uniform(-0.2, 0.2) if alpha == 1: m[0][1] = alpha elif alpha == 2: m[1][0] = alpha else: m = torch.mm(m, torch.FloatTensor( [[math.cos(alpha), math.sin(alpha)], [-math.sin(alpha), math.cos(alpha)]])) c = center + torch.FloatTensor(1, 2).uniform_(-8, 8) for stroke in char: stroke = stroke.float() / 255 - 0.5 stroke = c.expand_as(stroke) + \ torch.mm(stroke, m * (Scale - 0.01)) ############################################################### # To avoid GIL problems use a helper function: scn.dim_fn( 2, 'drawCurve')( inp.metadata.ffi, inp.features, stroke) ############################################################### # Above is equivalent to : # x1,x2,y1,y2,l=0,stroke[0][0],0,stroke[0][1],0 # for i in range(1,stroke.size(0)): # x1=x2 # y1=y2 # x2=stroke[i][0] # y2=stroke[i][1] # l=1e-10+((x2-x1)**2+(y2-y1)**2)**0.5 # v[1]=(x2-x1)/l # v[2]=(y2-y1)/l # l=max(x2-x1,y2-y1,x1-x2,y1-y2,0.9) # for j in numpy.arange(0,1,1/l): # p[0]=math.floor(x1*j+x2*(1-j)) # p[1]=math.floor(y1*j+y2*(1-j)) # inp.setLocation(p,v,False) ############################################################### inp.precomputeMetadata(precomputeStride) return {'input': inp, 'target': torch.LongTensor(tbl['target']) - 1}
def merge(tbl): inp = scn.InputBatch(2, spatial_size) center = spatial_size.float().view(1, 2) / 2 p = torch.LongTensor(2) v = torch.FloatTensor([1, 0, 0]) for char in tbl['input']: inp.addSample() for stroke in char: stroke = stroke.float() * (Scale - 0.01) / 255 - 0.5 * (Scale - 0.01) stroke += center.expand_as(stroke) scn.dim_fn(2, 'drawCurve')(inp.metadata.ffi, inp.features, stroke) inp.precomputeMetadata(precomputeStride) return {'input': inp, 'target': torch.LongTensor(tbl['target'])}
def run_sparse(img_tensor, out_chan, res_block=False, n_warmup=15, n_run=15): stream = torch.cuda.current_stream() input_size = img_tensor.shape batch = input_size[0] in_chan = input_size[1] h = input_size[2] w = input_size[3] if not res_block: sparseModel = submanifold_single_conv(in_chan, out_chan) else: sparseModel = submanifold_resnet_block(in_chan, out_chan) sparse_batch = scn.InputBatch(2, torch.LongTensor([h, w])) sparse_batch.addSample() count = 0 for y, x in np.ndindex((h, w)): val = img_tensor[0, :, y, x] if val[0] > 0.5: location = torch.LongTensor([y, x]) featureVector = torch.FloatTensor(val) sparse_batch.setLocation(location, featureVector, 0) count += 1 # warmup inp = sparse_batch.cuda() for i in range(n_warmup): out = sparseModel.forward(inp) cu_prof_start() # Use CUDA events for accurate timing. starte = torch.cuda.Event(enable_timing=True) ende = torch.cuda.Event(enable_timing=True) stream.record_event(starte) for i in range(n_run): out = sparseModel.forward(inp) stream.record_event(ende) ende.synchronize() evt_dt = starte.elapsed_time(ende) cu_prof_stop() sparse_ms = evt_dt / n_run return sparse_ms
def precomputeMetadata(input, scale, conv, batch_size, abc, group_num): input_size = input.getSpatialSize() input_groups = [] for scale_idx in range(scale): spatialSize = torch.LongTensor( [int(size / (2**scale_idx)) for size in input_size]) if conv[scale_idx] == 'g': #group locations = input.getSpatialLocations(spatialSize) locations = partition(locations, abc[0], abc[1], abc[2], group_num, batch_size) #precompute rules for spatial group convolution to save time group_input = scn.InputBatch(3, spatialSize) group_input.setLocations(locations, torch.Tensor(len(locations)).view(-1, 1)) group_input.getValidRules(spatialSize, 3) input_groups.append(group_input) else: input.getValidRules(spatialSize, 3) # compute rules for downsample convolution input.getConvRulesAndOutput(spatialSize, 2, 2) return input_groups
# Use the GPU if there is one, otherwise CPU dtype = 'torch.cuda.FloatTensor' if torch.cuda.is_available( ) else 'torch.FloatTensor' model = scn.Sequential().add( scn.SparseVggNet(2, 1, [['C', 8], ['C', 8], ['MP', 3, 2], ['C', 16], ['C', 16], ['MP', 3, 2], ['C', 24], ['C', 24], ['MP', 3, 2]])).add( scn.ValidConvolution(2, 24, 32, 3, False)).add( scn.BatchNormReLU(32)).add( scn.SparseToDense(2)).type(dtype) # output will be 10x10 inputSpatialSize = model.suggestInputSize(torch.LongTensor([10, 10])) input = scn.InputBatch(2, inputSpatialSize) msg = [ " X X XXX X X XX X X XX XXX X XXX ", " X X X X X X X X X X X X X X X X ", " XXXXX XX X X X X X X X X X XXX X X X ", " X X X X X X X X X X X X X X X X X X ", " X X XXX XXX XXX XX X X XX X X XXX XXX " ] input.addSample() for y, line in enumerate(msg): for x, c in enumerate(line): if c == 'X': location = torch.LongTensor([x, y]) featureVector = torch.FloatTensor([1]) input.setLocation(location, featureVector, 0)
def merge(tbl): merge_time = time.time() input = scn.InputBatch(3, spatialSize) target = [] target2 = [] batch_weight = [] batch_weight2 = [] count = 0 locations = np.empty((0, 3)).astype(int) vals = [] nz_nums = torch.LongTensor(train_batch_size) for input_nz, input_val, target_nz, target_val, weight_info in zip( tbl[b'input_nz'], tbl[b'input_val'], tbl[b'target_nz'], tbl[b'target_val'], tbl[b'weight']): locations = np.vstack((locations, input_nz)) vals = np.concatenate([vals, input_val]) nz_nums[count] = len(input_nz) label = np.zeros(dataset_outputSize.tolist()).astype(np.float32) label[target_nz.astype(int).tolist()] = target_val target.append(label) # compute downscale label label2 = SUNCGData.downsampleLabel2(label, 2).reshape( dataset_outputSize2.tolist()) target2.append(label2) # compute weight weight = np.zeros(dataset_outputSize.tolist()) weight[weight_info[b'hard_pos'].astype(int).tolist()] = 1 hard_pos_num = weight_info[b'hard_pos'].shape[1] easy_pos_num = min(int(hard_pos_num * easy_ratio), weight_info[b'easy_pos'].shape[1]) if easy_pos_num > 0: easy_pos_idx = np.random.permutation( weight_info[b'easy_pos'].shape[1])[:easy_pos_num] weight[weight_info[b'easy_pos'][:, easy_pos_idx].astype( int).tolist()] = 1 neg_num = int( min((hard_pos_num + easy_pos_num) * neg_ratio, weight_info[b'hard_neg'].shape[1] / (1 - easy_ratio))) hard_neg_num = int( min(neg_num * (1 - easy_ratio), weight_info[b'hard_neg'].shape[1])) if hard_neg_num > 0: hard_neg_idx = np.random.permutation( weight_info[b'hard_neg'].shape[1])[:hard_neg_num] weight[weight_info[b'hard_neg'][:, hard_neg_idx].astype( int).tolist()] = 1 easy_neg_num = neg_num - hard_neg_num if easy_neg_num > 0: easy_neg_idx = np.random.permutation( weight_info[b'easy_neg'].shape[1])[:easy_neg_num] weight[weight_info[b'easy_neg'][:, easy_neg_idx].astype( int).tolist()] = 1 batch_weight.append(weight) weight2 = weight.reshape([ dataset_outputSize2[0], 2, dataset_outputSize2[1], 2, dataset_outputSize2[2], 2 ]).mean(5).mean(3).mean(1) weight2[weight2 > 0] = 1 batch_weight2.append(weight2) count = count + 1 #put data at the center of the volume input.setInputBatchLocations(torch.from_numpy(locations).long()+input_offset.view(1,3), \ torch.FloatTensor(vals).view(nz_nums.sum(),1), nz_nums) precomputeStride = 2 input.precomputeMetadata(precomputeStride) return {'input': input, 'target': torch.LongTensor(np.array(target).astype(int)), 'weight':torch.FloatTensor(np.array(batch_weight)), \ 'target2': torch.LongTensor(np.array(target2).astype(int)), 'weight2':torch.FloatTensor(np.array(batch_weight2)) }