def _depth_first_break_cycles(graph, back_labels): visited = set() for s in list(graph.keys()): if s in visited: continue state_stack = stack() labels_stack = stack() state_stack.append(s) labels_stack.append( {s}) # a dummy label set, to pop at the end of the first loop while state_stack: labels = labels_stack.peek() state = state_stack.peek() if labels <= back_labels or state in visited: # edge was removed in a previous iteration # or state was already visited state_stack.pop() labels_stack.pop() continue visited.add(state) outgoing = graph.get(state, {}) for tgt, labels in list(outgoing.items()): if tgt not in visited: state_stack.append(tgt) labels_stack.append(labels) elif tgt in state_stack: # if tgt is visited and in state_stack, then there is a cycle! back_labels |= labels for var in list(labels): _remove_transitions_with_var(graph, var)
def mn(m, n, stages=3, strict_nonblocking=False, augment=False): assert stages % 2 == 1 if stages == 1: # return mxn full connection return [[range(n)] * m] # otherwise, build the network recursively n1, n2 = _solve(m, n, strict_nonblocking) k = (n1 + n2 - 1) if strict_nonblocking else max(n1, n2) r1 = cint(m * 1. / n1) r2 = cint(n * 1. / n2) layers = [] # Build the first stage blocks = [[[range(k)] * n1]] * r1 layers.extend(stack(*blocks)) # Connect the first stage with the core switch network layers.append(_xlink(r1, k)) # Recursively build the core switch network blocks = [mn(r1, r2, stages - 2, strict_nonblocking)] * k layers.extend(stack(*blocks)) # Connect the core switch network with the last stage layers.append(_xlink(k, r2)) # Build the last stage blocks = [[[range(n2)] * k]] * r2 layers.extend(stack(*blocks)) return layers
def slidingTopK(h, K, M, mask=None, stride=1): """ Performs KNN on each input pixel with a window of MxM. ONLY STRIDE==1 WORKS FOR NOW... """ if stride != 1: raise NotImplementedError # form index set that follows the reflection padding of input vector index = torch.arange(h.shape[-2] * h.shape[-1]).reshape( 1, 1, h.shape[-2], h.shape[-1]).float() index = utils.conv_pad(index, M, mode='reflect') hp = utils.conv_pad(h, M, mode='reflect') hs = utils.stack(hp, M, stride) # (B,I,J,C,M,M) B, I, J = hs.shape[:3] hbs = utils.batch_stack(hs) # (BIJ, C, M, M) ibs = utils.batch_stack(utils.stack(index, M, stride)) cpx = (M - 1) // 2 pad = (int(np.floor((stride - 1) / 2)), int(np.ceil((stride - 1) / 2))) v = hbs[..., (cpx - pad[0]):(cpx + pad[1] + 1), (cpx - pad[0]):(cpx + pad[1] + 1)] S = v.shape[-1] print(f"forming adjacency matrix...") G = graphAdj(v, hbs, mask) # (BIJ, SS, MM) ibs = ibs.reshape(B * I * J, 1, M * M) edge = torch.topk(G, K, largest=False).indices edge = edge + torch.arange(0, B * I * J, device=h.device).reshape( -1, 1, 1) * M * M edge = torch.index_select(ibs.reshape(-1, 1), 0, edge.flatten()) edge = edge.reshape(B * I * J, S * S, K).permute(0, 2, 1).reshape(-1, K, S, S) edge = utils.unbatch_stack(edge, (I, J)) edge = utils.unstack(edge) return edge.long()
def generate_general_reduced_rules(self, counter, i, j): """generate_general_reduced_rules Generate a middle rule, starting at ci and ending at cj :param counter: The counter to avoid collision :param i: index of the first relation :param j: index of the last relation """ rules = [] rules.append( DuplicationRule("C", "A" + str(counter), "D" + str(counter))) temp_counter = counter temp = stack(["end"] + self.part1[:i], counter + 1, "A" + str(counter), "C") counter = temp[1] rules = rules + temp[0] temp = unstack(self.part0[i:j + 1], self.part0, temp_counter, "D" + str(temp_counter), "Cback" + str(temp_counter + self.n_relations() + 1)) counter = temp[1] rules = rules + temp[0] counter = max(counter, temp_counter) counter += 1 temp = stack(self.part1[j + 1:], counter, "Cback" + str(temp_counter + self.n_relations() + 1), "C") counter = temp[1] rules = rules + temp[0] return (rules, counter)
def process_batch(batch, loss, i, k, set_, t0): """Optimization step. batch = [input, target]: contains data for optim step [input, target] loss: dict containing statistics about optimization i: epoch k: index of the current batch set_: type of batch (\"train\" or \"dev\") t0: time of the beginning of epoch """ nbatch = vars(opt)['nbatch_' + set_] res = model.step(batch, set_) for key, value in res.items(): try: loss[key].append(value) except KeyError: loss[key] = [value] if opt.verbose: batch_time = (time.time() - t0) / (k + 1) eta = nbatch * batch_time out = ' %s %d: batch %.5d/%.5d |' %(set_, i, k, nbatch - 1) for key, value in res.items(): out += ' %s: %.2e |' %(key, value) out += ' batch time: %.2fs | %s eta: %.2dH%.2dm%.2ds' \ %(batch_time, set_, eta / (60 * 60), (eta / 60) % 60, eta % 60) print(out, end='\r') if opt.image_save or opt.visdom: if 'bev' not in opt.input: to_plot = [] nviz = min(10, opt.bsz) to_plot.append(utils.stack(batch[0], nviz, opt.input_len)) to_plot.append(utils.stack(batch[1], nviz, opt.target_len)) to_plot.append(utils.stack(model.output(), nviz, opt.target_len)) img = np.concatenate(to_plot, 2) elif opt.input == 'bev-depth': to_plot = [] bev, fv, bev_targets, fv_targets = utils.bev_batch_viz(batch) to_plot.append((bev, 'BEV')) to_plot.append((fv, 'FV')) to_plot.append((bev_targets, 'BEV targets')) to_plot.append((fv_targets, 'FV targets')) to_plot.append((model.output(), 'BEV and FV predictions')) img = to_plot elif opt.input == 'bev-crop': img = [] bev_crop, fv_crop, fv_full, label = utils.bev_crop_viz(batch) #img.append(bev_crop) img.append((fv_full, label)) img.append((fv_crop, label)) elif opt.input == 'bev-prior': img = [] bev = utils.bev_prior_viz(batch) img.append((bev, 'BEV')) img.append((model.output(), 'prior and posterior')) viz(img, loss, i, k, nbatch, set_) return loss
def process_batch(batch, j, t0): """Compute score for every frames in a video (which is also a batch). batch = [input, target]: all frames in the video j: index of the video t0: time when the test started """ nbatch = vars(opt)["nbatch_test"] frame_scores = np.zeros((opt.m, 4)) d3, d4 = batch[0].size(3), batch[0].size(4) for i in range(opt.bsz): for c in range(4): data = [batch[0][i][c], batch[1][i][c]] frame_scores[i][c] = model.score(data) if opt_test.image_save or opt_test.visdom: to_plot = [] nviz = 1 to_plot.append(utils.stack(data[0].unsqueeze(0), nviz, opt.input_len)) to_plot.append(utils.stack(data[1].unsqueeze(0), nviz, opt.target_len)) to_plot.append(utils.stack(model.output(), nviz, opt.target_len)) img = np.concatenate(to_plot, 2) viz_output(img, {}, i, j, nbatch, "output") if opt_test.verbose: batch_time = (dt.time() - t0) / (j + 1) eta = nbatch * batch_time out = " test: batch %.5d/%.5d |" % (j, nbatch - 1) mean = frame_scores.mean(0) out += " Mean: %.2e - %.2e - %.2e - %.2e |" % ( mean[0], mean[1], mean[2], mean[3], ) out += " batch time: %.2fs | test eta: %.2dH%.2dm%.2ds" % ( batch_time, eta / (60 * 60), (eta / 60) % 60, eta % 60, ) print(out, end="\r") if opt_test.image_save or opt_test.visdom: for c in range(4): to_plot = [] nviz = opt.m to_plot.append(utils.stack(batch[0].select(1, c), nviz, opt.input_len)) to_plot.append(utils.stack(batch[1].select(1, c), nviz, opt.target_len)) img = np.concatenate(to_plot, 2) viz(img, {"c": frame_scores}, i, j, nbatch, str(c)) return frame_scores
def generate_splitting(self, counter, first_nt, also): """generate_splitting Generate the splitting for the branches :param counter: The first index to use to generate rules :param first_nt: the first non terminal of the tree (the head) """ rules = [] initial_counter = counter if len(self.others) == 0: rules.append(DuplicationRule("C", "T", first_nt)) elif len(self.others) == 1: rules.append(DuplicationRule("C", "CD" + str(counter), first_nt)) else: rules.append( DuplicationRule("C", "CD" + str(counter), "K" + str(counter + 1))) counter += 1 for i in range(len(self.others) - 2): rules.append( DuplicationRule("K" + str(counter), "CD" + str(counter), "K" + str(counter + 1))) counter += 1 rules.append( DuplicationRule("K" + str(counter), "CD" + str(counter), first_nt)) # Stacking for i in range(len(self.others)): temp = stack(["end"] + self.others[i].part0 + also, counter, "CD" + str(initial_counter + i), "C") counter = temp[1] rules = rules + temp[0] return (rules, counter)
def resolve_cycles(self, assignments): visited = set() for s in self.nodes: if s in visited: continue node_stack = stack() node_stack.append(s) while node_stack: node = node_stack.peek() if node in visited: # node was already visited node_stack.pop() continue visited.add(node) targets = s.children for target in targets: if target not in visited: node_stack.append(target) elif target in node_stack: # if target is visited and in state_stack, then there is a cycle! # NOTE: it would be sufficient to find all elements in the cycle # and put these together in a composite # for now we keep the original composite intact return True return False
def _build_layer(n, bases, direct, overlay, augment, exchange_func, next_base): # Build the shuffle layer shuffle = [] for x in xrange(n): xseq = num_to_seq(x, bases) out = set() # if direct is set, point to itself # otherwise, <<1, shuffle = Exchange(Omega(X, 1)) # if overlay is set, do it for LogN times for k in xrange(0 if direct else 1, len(bases) if overlay else 2): shifted_bases = bases[-k:] + bases[:-k] yseq = [exchange_func(b, d) for b, d in zip(xseq[-k:], bases[-k:])] + xseq[:-k] # Exchange(x<<k) if augment: # connect to every node in a block out.update([seq_to_num([i] + yseq[1:], shifted_bases) for i in xrange(shifted_bases[0])]) else: # just add one node in a block out.add(seq_to_num(yseq, shifted_bases)) shuffle.append(list(out)) # Build the switcher layer shifted_bases = bases[-1:] + bases[:-1] switcher = [] num_blocks = reduce(lambda x, y: x*y, shifted_bases[1:], 1) blocks = [[[range(next_base)] * shifted_bases[0]]] * num_blocks switcher = stack(*blocks)[0] return [shuffle, switcher]
def forward(self, names: Iterator[Name]) -> TT: """The forward calculation of the name's language recognition model. Args: names: a sequence of person names; calculating the scores for several names at the same time is faster thanks to better parallelization Returns: score matrix in which each row corresponds to a single name, with its individual elements corresponding to the scores of different languages """ # TODO EX2 (a): the following lines need to be adapted to the EmbeddingSum, # which processes features in groups. You will also need to make # trivial modifications in the code in two or three other places # (imports, initialization). # TODO EX2 (b): you can further try to modify the EmbeddingSum class so # that it works over batches of feature groups. embeddings = [ # [self.emb.forward(feat) for feat in self.features(name)] self.emb.forward(self.features(name)) for name in names ] # cbow = utils.from_rows(map(sum, embeddings)) cbow = utils.stack(embeddings) scores = self.ffn.forward(cbow) return scores
def encode(self, unencode_boxes, reference_boxes): #np.ones(5, dtype=np.float32)): ''' :param unencode_boxes: [batch_size*H*W*num_anchors_per_location, 5] :param reference_boxes: [H*W*num_anchors_per_location, 5] :return: encode_boxes [-1, 5] # xc,yc,w,h,theta ''' weights = self.weights lib = self.lib x_center, y_center, w, h, theta = \ unencode_boxes[:, 0], unencode_boxes[:, 1], unencode_boxes[:, 2], unencode_boxes[:, 3], unencode_boxes[:, 4] reference_x_center, reference_y_center, reference_w, reference_h, reference_theta = \ reference_boxes[:, 0], reference_boxes[:, 1], reference_boxes[:, 2], reference_boxes[:, 3], reference_boxes[:, 4] reference_w += EPSILON reference_h += EPSILON w += EPSILON h += EPSILON # to avoid NaN in division and log below t_xcenter = (x_center - reference_x_center) / reference_w t_ycenter = (y_center - reference_y_center) / reference_h t_w = lib.log(w / reference_w) t_h = lib.log(h / reference_h) """ TO PREVENT angle AMBIGUITY for targets where the height and width are roughly similar, there may be ambiguity in angle regression e.g. if height and width are equal, angle regression could be -90 or 0 degrees we don't want to penalize this # """ # THRESH = 0.15 # w_to_h_ratio = w / h # w_to_h_ratio_diff = self.lib.abs(1.0 - w_to_h_ratio) # adj_theta = theta.clone() if self.lib == torch else theta.copy() # square_ids = w_to_h_ratio_diff < THRESH # adj_squares_theta = adj_theta[square_ids] # adj_squares_theta[adj_squares_theta > 90] -= 90 # adj_squares_theta[adj_squares_theta > 45] -= 90 # adj_theta[square_ids] = adj_squares_theta t_theta = theta - reference_theta # t_theta[t_theta > 90] -= 90 # t_theta[t_theta > 45] -= 90 t_theta = t_theta * np.pi / 180 # convert to radians if weights is not None: wx, wy, ww, wh, wa = weights t_xcenter *= wx t_ycenter *= wy t_w *= ww t_h *= wh t_theta *= wa encode_boxes = stack([t_xcenter, t_ycenter, t_w, t_h, t_theta], dim=1, lib=lib) return encode_boxes
def process_batch(batch, loss, i, k, set_, t0): """Optimization step. batch = [input, target]: contains data for optim step [input, target] loss: dict containing statistics about optimization i: epoch k: index of the current batch set_: type of batch (\"train\" or \"dev\") t0: time of the beginning of epoch """ nbatch = vars(opt)["nbatch_" + set_] res = model.step(batch, set_) for key, value in res.items(): try: loss[key].append(value) except KeyError: loss[key] = [value] if opt.verbose: batch_time = (time.time() - t0) / (k + 1) eta = nbatch * batch_time out = " %s %d: batch %.5d/%.5d |" % (set_, i, k, nbatch - 1) for key, value in res.items(): out += " %s: %.2e |" % (key, value) out += " batch time: %.2fs | %s eta: %.2dH%.2dm%.2ds" % ( batch_time, set_, eta / (60 * 60), (eta / 60) % 60, eta % 60, ) print(out, end="\r") if opt.image_save or opt.visdom: to_plot = [] nviz = min(10, opt.bsz) to_plot.append(utils.stack(batch[0], nviz, opt.input_len)) to_plot.append(utils.stack(batch[1], nviz, opt.target_len)) to_plot.append(utils.stack(model.output(), nviz, opt.target_len)) img = np.concatenate(to_plot, 2) viz(img, loss, i, k, nbatch, set_) return loss
def generate_palindrome_rules(self, counter, susie=False): rules = [] temp = stack(self.part1, counter, "Cforward", "Cforward") counter = temp[1] rules = rules + temp[0] last = "Cbackward" if susie: last = "Cend" temp = unstack(self.part0, self.part0, counter, "Cbackward", last) counter = temp[1] rules = rules + temp[0] return (rules, counter)
def generate_palindrome_rules(self, counter, susie): rules = [] if self.n_inputs < 2: temp = stack(self.part1, counter, "Cforward", "Cforward") counter = temp[1] rules = rules + temp[0] else: c_temp = "C_inter" + str(counter) counter += 1 temp = unstack(self.part1[self.n_inputs - 2::-1], self.part1, counter, "Cforward", c_temp) counter = temp[1] rules = rules + temp[0] temp = stack(self.part1, counter, c_temp, "Cforward") counter = temp[1] rules = rules + temp[0] last = "Cbackward" if susie: last = "Cend" temp = unstack(self.part0, self.part0, counter, "Cbackward", last) counter = temp[1] rules = rules + temp[0] return (rules, counter)
def decode(self, encode_boxes, reference_boxes): ''' :param encode_boxes:[N, 5] # xc,yc,w,h,theta :param reference_boxes: [N, 5] # xc,yc,w,h,theta :param scale_factors: use for scale in the rpn stage, reference_boxes are anchors in the fast_rcnn stage, reference boxes are proposals(decode) produced by rpn stage :return:decode boxes [N, 5] ''' weights = self.weights lib = self.lib t_xcenter = encode_boxes[:, 0] t_ycenter = encode_boxes[:, 1] t_w = encode_boxes[:, 2] t_h = encode_boxes[:, 3] t_theta = encode_boxes[:, 4] if weights is not None: wx, wy, ww, wh, wa = weights t_xcenter /= wx t_ycenter /= wy t_w /= ww t_h /= wh t_theta /= wa dw = clamp(t_w, max=self.bbox_xform_clip, lib=lib) dh = clamp(t_h, max=self.bbox_xform_clip, lib=lib) reference_x_center = reference_boxes[:, 0] reference_y_center = reference_boxes[:, 1] reference_w = reference_boxes[:, 2] reference_h = reference_boxes[:, 3] reference_theta = reference_boxes[:, 4] predict_x_center = t_xcenter * reference_w + reference_x_center predict_y_center = t_ycenter * reference_h + reference_y_center predict_w = lib.exp(dw) * reference_w predict_h = lib.exp(dh) * reference_h predict_theta = t_theta * 180 / np.pi + reference_theta # radians to degrees decode_boxes = stack([ predict_x_center, predict_y_center, predict_w, predict_h, predict_theta ], dim=1, lib=lib) return decode_boxes
def generate_left_reduced_rules(self, counter): """generate_left_reduced_rules Generates the reduced left rules as describe in the paper. :param counter: counter used to be sure we do not duplicate non-terminals. So, it MUST be update after the function. :return A couple (rules, counter) containing the generated rules and the new counter value """ rules = [] for i in range(1, self.n_relations() + 1): temp = unstack(self.part0[0:i], self.part0, counter, "C", "Cback" + str(counter + self.n_relations() + 1)) counter = temp[1] rules = rules + temp[0] temp = stack(self.part1[i:], counter, "Cback" + str(counter), "C") counter = temp[1] rules = rules + temp[0] return (rules, counter)
def windowedTopK(h, K, M, mask): """ Returns top K feature vector indices for h: (B, C, H, W) input feature M: window side-length mask: (H*W, H*W) Graph mask. output: (B, K, H, W) K edge indices (of flattened image) for each pixel """ # stack image windows hs = utils.stack(h, M, M) # (B,I,J,C,M,M) I, J = hs.shape[1], hs.shape[2] # move stack to match dimension to build batched Graph Adjacency matrices hbs = utils.batch_stack(hs) # (B*I*J,C,M,M) G = graphAdj(hbs, hbs, mask) # (B*I*J, M*M, M*M) # find topK in each window, unbatch the stack, translate window-index to tile index # (B*I*J,M*M,K) -> (B*I*J,K,M*M) -> (B*I*J, K, M, M) edge = torch.topk(G, K, largest=False).indices.permute(0, 2, 1).reshape(-1, K, M, M) edge = utils.unbatch_stack(edge, (I, J)) # (B,I,J,K,M,M) return utils.indexTranslate(edge, M) # (B,K,H,W)
def propagate(self, aggr, edge_index, **kwargs): r"""The initial call to start propagating messages. Takes in an aggregation scheme (:obj:`"add"`, :obj:`"mean"` or :obj:`"max"`), the edge indices, and all additional data which is needed to construct messages and to update node embeddings.""" assert aggr in ['split_stack', 'stack', 'add', 'mean', 'max'] kwargs['edge_index'] = edge_index size = None message_args = [] for arg in self.__user_args__: if arg[-2:] == '_i': # tmp is x tmp = kwargs[arg[:-2]] size = tmp.size(0) message_args.append(tmp[edge_index[0]]) elif arg[-2:] == '_j': tmp = kwargs[arg[:-2]] size = tmp.size(0) message_args.append(tmp[edge_index[1]]) else: message_args.append(kwargs[arg]) update_args = set(self.__update_params__.keys()) update_args = [kwargs[arg] for arg in update_args] out = self.message(*message_args) if aggr == 'split_stack': out = split_stack(out, edge_index[0], kwargs['edge_type'], dim_size=size) elif aggr == 'stack': out = stack(out, edge_index[0], kwargs['edge_type'], dim_size=size) else: out = scatter_(aggr, out, edge_index[0], dim_size=size) out = self.update(out, *update_args) return out
def generate_right_reduced_rules(self, counter): """generate_right_reduced_rules Generates the reduced right rules as describe in the paper. :param counter: counter used to be sure we do not duplicate non-terminals. So, it MUST be update after the function. :return A couple (rules, counter) containing the generated rules and the new counter value """ rules = [] for i in range(1, self.n_relations()): rules.append( DuplicationRule("C", "A" + str(counter), "D" + str(counter))) temp_counter = counter temp = stack(["end"] + self.part1[:i], counter, "A" + str(counter), "C") counter = temp[1] rules = rules + temp[0] temp = unstack(self.part0[i:self.n_relations()], self.part0, temp_counter, "D" + str(temp_counter), "C") counter = temp[1] rules = rules + temp[0] counter = max(counter, temp_counter) counter += 1 return (rules, counter)
frame = frame / float(255) # normalize frame = frame.astype('float32') # cv2.imshow('frame',frame) if gray: frame = np.tile(frame, (1, 1, 1)) # give it the channel dim video.append(frame) # import time # time.sleep(0.5) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() video = u.stack(video) action_vids[ vid] = video # video, subsampled, evenly spaced, consecutive video pbar.finish() u.save_dict_to_hdf5(dataset=action_vids, dataset_name=action + '_subsamp=' + str(subsample), dataset_folder=out) # action_vids = np.vstack(action_vids) # consecutive video ACTUALLY THIS MIGHT NOT BE TRUE. WE NEED THE VIDEOS TO BE SEPARATE! # randomly permute -- don't do this! # tm1s = np.random.permutation(range(0,len(action_vids)-1,2)) # ts = np.array([i+1 for i in tm1s]) # shuffle_idxs = list(it.next() for it in itertools.cycle([iter(tm1s), iter(ts)])) # groups of 2 # action_vids = action_vids[np.array(shuffle_idxs),:,:]
def push_tree(self, first: str, last: str, counter: int, end: bool = False) -> Tuple[List[ReducedRule], int]: """push_tree Push the regex on the stack of the grammar. :param first: The firts non-terminal to use :param last: The last non-terminal to use :param counter: A counter to prevent duplication of non-terminals :param end: Whether an end symbol should be added to the stack :type first: str :type last: str :type counter: int :type end: boolean :return: A couple which contains first the rules generated and then\ the next counter to use :rtype: A couple of a list of Reduced rules and an int """ rules = [] # We begin by pushing the end symbol if necessary if end: rules.append(ProductionRule(first, "Ce" + str(counter), "end")) first = "Ce" + str(counter) if not self.head.is_str(): # i.e. we have a function res = utils.stack(self.head.get_function().part1, counter, first, last) rules += res[0] counter = res[1] return (rules, counter) elif self.head.get_str() == ".": # Concatenation # Creates intermediate non-terminals temp_nt = [first] for _ in range(len(self.sons)): temp_nt.append("C" + str(counter)) counter += 1 # Consume each son separately and join them with the intermediate # non-terminals for i in range(len(self.sons)): temp = self.sons[i].push_tree(temp_nt[i], temp_nt[i + 1], counter) rules += temp[0] counter = temp[1] # link last intermediate to the last non-symbol rules.append(DuplicationRule(temp_nt[-1], "T", last)) return (rules, counter) elif self.head.get_str() == "|": # Or node # Each son is pushed separately and they all have the same first # and last non-terminals for son in self.sons: temp = son.push_tree(first, last, counter) rules += temp[0] counter = temp[1] return (rules, counter) elif self.head.get_str() == "*": # Kleene star # We make the first symbol go directly to the last one, to simulate # the empty case rules.append(DuplicationRule(first, "T", last)) # Normally just one for son in self.sons: # We should end where we begin temp = son.push_tree(last, last, counter) rules += temp[0] counter = temp[1] return (rules, counter) return (rules, counter)
if gray: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # shape: (height, width) it is gray anyway if i % subsample == 0: frame = frame/float(255) # normalize frame = frame.astype('float32') # cv2.imshow('frame',frame) if gray: frame = np.tile(frame,(1,1,1)) # give it the channel dim video.append(frame) # import time # time.sleep(0.5) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() video = u.stack(video) action_vids[vid] = video # video, subsampled, evenly spaced, consecutive video pbar.finish() u.save_dict_to_hdf5(dataset=action_vids, dataset_name=action+'_subsamp='+str(subsample), dataset_folder=out) # action_vids = np.vstack(action_vids) # consecutive video ACTUALLY THIS MIGHT NOT BE TRUE. WE NEED THE VIDEOS TO BE SEPARATE! # randomly permute -- don't do this! # tm1s = np.random.permutation(range(0,len(action_vids)-1,2)) # ts = np.array([i+1 for i in tm1s]) # shuffle_idxs = list(it.next() for it in itertools.cycle([iter(tm1s), iter(ts)])) # groups of 2 # action_vids = action_vids[np.array(shuffle_idxs),:,:] # action_data[action] = action_vids # save