def print_dialogue_features(modeldata: ModelData, num_batches: int, num_sentences: int): inputs, responses, targets = [], [], [] for *x, y in iter(modeldata.trn_dl): inputs.append(to_np(x[0])) responses.append(to_np(x[1])) targets.append(to_np(y)) for batch_num, (input, response, target) in enumerate(zip(inputs, responses, targets)): input = np.transpose( input, [1, 2, 0]) # transpose number of utterances to beams [sl, bs, nb] inputs_str = modeldata.itos(input, "text") inputs_str = ["\n".join(conv) for conv in inputs_str] targets_str = modeldata.itos(target, "text") response_str = modeldata.itos(response, "text") for index, (inp, resp, targ) in enumerate( zip(inputs_str, response_str, targets_str)): print( f'BATCH: {batch_num} SAMPLE : {index}\nINPUT:\n{"".join(inp)}, {len(inp.split())}\nRESPONSE:\n{"".join(resp)}, {len(resp[0].split())}\nTARGET:\n{ "".join(targ)}, {len(targ[0].split())}\n\n' ) if 0 < num_sentences <= index - 1: break if 0 < num_batches <= batch_num - 1: break
def test_to_np(): array = np.arange(0, 3).astype(np.float) assert core.to_np(array) is array tensor = core.T(array) result = core.to_np([tensor, tensor]) np.testing.assert_equal(result[0], array) np.testing.assert_equal(result[1], array) variable = core.V(array) np.testing.assert_equal(core.to_np(variable), array)
def predict_with_seq2seq(m, dl, num_beams=1): m.eval() if hasattr(m, 'reset'): m.reset() inputs, predictions, targets = [], [], [] for *x, y in iter(dl): inputs.append(to_np(x[0])) targets.append(to_np(y)) prediction, *_ = m(*VV(x), num_beams=num_beams) predictions.append(to_np(prediction)) return predictions, targets, inputs
def aucs(preds, targets): targets = to_np(targets) preds = to_np(preds) aurocs = [] n = preds.shape[1] print(n) # preds = np.nan_to_num(preds) # some numpy was nan or inf for i in range(n): aurocs.append(roc_auc_score(targets[:, i], preds[:, i])) return T(aurocs)
def test_attention_projection(attention_projection_setup): encoder_outputs, decoder_output, params = attention_projection_setup module = to_gpu(AttentionProjection(**params)) # When I reset the module module.reset(keys=encoder_outputs) # the attention output will be a zeros array with shape equal to the input assert to_np(module.get_attention_output(decoder_output)).sum() == 0 assert module.get_attention_output(decoder_output) is not module._attention_output # when when I pass an input for the the decoder output results = module(decoder_output) assert_dims(results, [1, 2, params['n_out']]) # the new attention_output is calculated from he attention module and is no longer zero assert to_np(module.get_attention_output(decoder_output)).sum() != 0 assert module.get_attention_output(decoder_output) is module._attention_output assert_dims(module._attention_output, [2, params['n_in']])
def make_predictions(): try: content = request.get_json(force=True) except HTTPException as e: return jsonify({'error': 'Request data invalid'}), 400 img_str = base64.b64decode(str(content['image'])) nparr = np.fromstring(img_str, np.uint8) img = cv2.imdecode(nparr, cv2.IMREAD_COLOR).astype(np.float32) / 255 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) height, width, channels = img.shape im = val_tfms(img) output = model(V(im[None])) output = to_np(output) bb_i = expit(output[:, :4]) y, x, y2, x2 = bb_i[0] bb_scaled = [y * height, x * width, y2 * height, x2 * width] bb_np = bb_hw(bb_scaled) c_i = output[:, 4:] class_pred = itoa[np.argmax(c_i)] return jsonify({'class': class_pred, 'bb': list([int(b) for b in bb_np])})
def predict(self, x): fake_labels = np.array([0] * len(x)) ds = TextDataset(x, fake_labels) dl = DataLoader(ds, 1000, transpose=True, num_workers=1, pad_idx=1) preds = predict(self.m, dl) sm = Softmax() return to_np(sm(V(T(preds))))
def plot_detect_pred(img, pred_clas, pred_bbox, clas, bbox, anchors, anc_sizes, sz, cls_idx=0, thresh=0.5, do_nms=True, figsize=(20, 20)): # activation to bbox corner a_ic = actn_to_bb(pred_bbox, anchors, anc_sizes) # copy bbox actual_bb = to_np(bbox).copy() # get probabilities pred_proba = to_np(F.softmax(pred_clas))[:, cls_idx] # get thresholded predictions pred_bbox_mask = pred_proba > thresh pred_bbox_probas = np.round(pred_proba[pred_bbox_mask], 2) pred_bbox_corner = to_np(a_ic * sz).astype(int)[pred_bbox_mask] # do non max suppresion if do_nms: # remaining boxes post_nms_idxs = [] # non max supression sorted_idx = np.argsort(pred_bbox_probas)[::-1] post_nms_idxs = nms(sorted_idx, pred_bbox_corner) pred_bbox_corner = pred_bbox_corner[post_nms_idxs] pred_bbox_probas = pred_bbox_probas[post_nms_idxs] # actual bbox actual_bbox = actual_bb * sz # plot fig, axes = plt.subplots(1, 1, figsize=figsize) for i, (bbox, proba) in enumerate(zip(pred_bbox_corner, pred_bbox_probas)): draw_bbox(bbox, axes) draw_text(axes, bbox[:2][::-1], f"pred_{i}") draw_text(axes, bbox[:2][::-1], f"proba:{proba}") for i, bbox in enumerate(actual_bbox): draw_bbox(bbox, axes, "white") draw_text(axes, bbox[:2][::-1], f"gt_{i}") axes.imshow(img)
def neighbor_gen(at, distance_expansion=None, cutoff=5.0, n_gaussians=25, trainable_gaussians=False, environment_provider=ASEEnvironmentProvider(5.0), collect_triples=False, pair_provider=None, center_positions=True): properties = {} properties[Structure.Z] = T(at.numbers.astype(np.int)).unsqueeze(0) positions = at.positions.astype(np.float32) if center_positions: positions -= at.get_center_of_mass() properties[Structure.R] = T(positions).unsqueeze(0) properties[Structure.cell] = T(at.cell.astype(np.float32)).unsqueeze(0) # get atom environment idx = 0 nbh_idx, offsets = environment_provider.get_environment(idx, at) properties[Structure.neighbors] = T(nbh_idx.astype(np.int)).unsqueeze(0) properties[Structure.cell_offset] = T(offsets.astype( np.float32)).unsqueeze(0) properties[Structure.neighbor_mask] = None properties['_idx'] = T(np.array([idx], dtype=np.int)).unsqueeze(0) if pair_provider is not None: nbh_idx_j, nbh_idx_k = pair_provider.get_environment(nbh_idx) properties[Structure.neighbor_pairs_j] = T(nbh_idx_j.astype(np.int)) properties[Structure.neighbor_pairs_k] = T(nbh_idx_k.astype(np.int)) model = spk.custom.representation.RBF( distance_expansion=distance_expansion, cutoff=cutoff, n_gaussians=n_gaussians, trainable_gaussians=trainable_gaussians) model = to_gpu(model) r, f = model.forward(properties) return to_np(r.squeeze()), to_np(f.squeeze())
def test_to_np(): array = np.arange(0, 3).astype(np.float) assert core.to_np(array) is array tensor = core.T(array) result = core.to_np([tensor, tensor]) np.testing.assert_equal(result[0], array) np.testing.assert_equal(result[1], array) variable = core.V(array) np.testing.assert_equal(core.to_np(variable), array) with mock.patch("torch.cuda") as cuda_mock: tensor_long = core.T(array.astype(np.int)) cuda_mock.is_available(return_value=True) cuda_mock.HalfTensor = torch.LongTensor array = core.to_np(tensor_long) np.testing.assert_equal(array, [0., 1., 2.]) assert array.dtype in (np.float32, np.float64)
def test_to_np(): array = np.arange(0, 3).astype(np.float) assert core.to_np(array) is array tensor = core.T(array) result = core.to_np([tensor, tensor]) np.testing.assert_equal(result[0], array) np.testing.assert_equal(result[1], array) variable = core.V(array) np.testing.assert_equal(core.to_np(variable), array) with mock.patch("torch.cuda.is_available") as is_available_mock: with mock.patch("fastai.core.is_half_tensor") as is_half_tensor_mock: is_available_mock.return_value=True is_half_tensor_mock.return_value=True tensor = core.T(array.astype(np.int)) array = core.to_np(tensor) np.testing.assert_equal(array, [0., 1., 2.]) assert array.dtype in (np.float32, np.float64)
def test_select_hidden_by_index(): bs, num_beams = 2, 3 # when I pass inputs to the select_hidden_by_index function with bs=2, num_beams = 3 inputs = np.array([2, 3, 4, 10, 11, 12]).reshape(1, 6, 1) # [ndir, bs, hd] tr_inputs = [V(T(inputs))] # and indices for every batch [bs, ndims] indices = np.array([[0, 0, 1], [2, 2, 2]]) tr_indices = V(T(indices)) tr_indices = reshape_parent_indices(tr_indices.view(-1), bs=bs, num_beams=num_beams) results = select_hidden_by_index(tr_inputs, tr_indices.view(-1)) # then I get the expected seletec hidden expected = np.array([2, 2, 3, 12, 12, 12]) assert_allclose(actual=to_np(results[0]).ravel(), desired=expected)
def test_to_np(): array = np.arange(0, 3).astype(np.float) assert core.to_np(array) is array tensor = core.T(array) result = core.to_np([tensor, tensor]) np.testing.assert_equal(result[0], array) np.testing.assert_equal(result[1], array) variable = core.V(array) np.testing.assert_equal(core.to_np(variable), array) with mock.patch("torch.cuda.is_available") as is_available_mock: with mock.patch("fastai.core.is_half_tensor") as is_half_tensor_mock: is_available_mock.return_value = True is_half_tensor_mock.return_value = True tensor = core.T(array.astype(np.int)) array = core.to_np(tensor) np.testing.assert_equal(array, [0., 1., 2.]) assert array.dtype in (np.float32, np.float64)
def make_predictions(): try: content = request.data #content = request.get_json(force=True) #content = format(content) print ('content is',format(content)) except HTTPException as e: print("Inside make predictions") return jsonify({'error': 'Request data invalid'}), 400 #print("RIMIIIIIIIII", content) content = content.decode().split(',')[1] print(content) img_str = base64.b64decode(str(content)) print('img_str is', img_str) nparr = np.fromstring(img_str, np.uint8) print('nparr is', nparr) imd = cv2.imdecode(nparr, cv2.IMREAD_COLOR) print('imdecode is',imd) img = imd.astype(np.float32) / 255 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) print('image is', img) height, width, channels = img.shape im = val_tfms(img) output = model(V(torch.from_numpy(im[None]))) print ("Rimiiiiiii", output) output = to_np(output) print ("Output", output) bb_i = expit(output[:, :4]) y, x, y2, x2 = bb_i[0] bb_scaled = [ y * height, x * width, y2 * height, x2 * width] bb_np = bb_hw(bb_scaled) c_i = output[:, 4:] print ("c_i", c_i) class_pred = itoa[np.argmax(c_i)] print ("class_pred", class_pred) print("list", list([int(b) for b in bb_np])) return jsonify({'class': class_pred, 'bb': list([int(b) for b in bb_np])})
def test_T(): tensor = torch.ones([1, 2]) np.testing.assert_equal(core.to_np(core.T(tensor)), [[1, 1]]) array = np.arange(0, 5) assert core.T(array.astype(np.int)).type() == "torch.LongTensor" assert core.T(array.astype(np.float)).type() == "torch.FloatTensor" with mock.patch("fastai.core.to_half") as to_half_mock: core.T(array.astype(np.float), half=True) to_half_mock.assert_called_once() with pytest.raises(NotImplementedError): assert core.T(array.astype(np.object))
def print_features(modeldata: ModelData, num_batches=1, num_sentences=-1): inputs, responses, targets = [], [], [] for *x, y in iter(modeldata.trn_dl): inputs.append(to_np(x[0])) responses.append(to_np(x[1])) targets.append(to_np(y)) for batch_num, (input, target, response) in enumerate(zip(inputs, targets, responses)): inputs_str: BatchBeamTokens = modeldata.itos( input, modeldata.trn_dl.source_names[0]) response_str: BatchBeamTokens = modeldata.itos( response, modeldata.trn_dl.source_names[1]) targets_str: BatchBeamTokens = modeldata.itos( target, modeldata.trn_dl.target_names[0]) for index, (inp, targ, resp) in enumerate( zip(inputs_str, targets_str, response_str)): print( f'batch: {batch_num} sample : {index}\ninput: {" ".join(inp)}\ntarget: { " ".join(targ)}\nresponse: {" ".join(resp)}\n\n' ) if 0 < num_sentences <= index - 1: break if 0 < num_batches <= batch_num - 1: break
def from_prob(self, prob, w, h): """ input: prob: np.array (14) output: heatmap np.array (h, w, 1) """ pred_y = np.argmax(prob) heatmap = self.mapping(pred_y) # single image heatmap = to_np(heatmap) # heatmap = ((heatmap - heatmap.min())*(1./(heatmap.max() - # heatmap.min()) * 255.)).astype(np.uint8) heatmap -= heatmap.min() heatmap /= heatmap.max() heatmap = cv2.resize(heatmap, (w, h)) # multiple image # h, w = heatmap.shape # heatmap = heatmap.view(bs, h*w) # heatmap -= heatmap.min(1, keepdim=True)[0] # heatmap /= heatmap.max(1, keepdim=True)[0] # heatmap = heatmap.view(bs, h, w) return heatmap, np.take(CLASS_NAMES, to_np(pred_y))
def crop_attention(heatmap, image_name): """ heatmap: (7, 7) range [0, 1] image_name: 00007551_000.png, 00000001_000.png """ image = Image.open(PATH/IMAGE_DN/image_name) heatmap = to_np(heatmap) heatmap = cv2.resize(heatmap, (1024, 1024)) mask = heatmap > 0.7 slice_y, slice_x = ndimage.find_objects(mask, True)[0] image = np.array(image) cropped = Image.fromarray(image[slice_y, slice_x]) cropped.save(attention_dn/image_name)
def test_S2SModelData_from_file(generalmodel): assert generalmodel is not None # number of batches assert 200 == len(generalmodel.trn_dl) train_iter = iter(generalmodel.trn_dl) batch = next(train_iter) assert isinstance(batch, list) # shape should be equal to sl, bs # The elements in the batch equal the sum of source_names and target_names (in this case 4) # the first three being the sources (inputs to the encoder, and the last the target_names (input to the decoder) assert_dims(batch, [4, None, 2]) sentences = to_np(batch[0]) batch_sentences = generalmodel.itos(sentences, "english") for beam_sentence in batch_sentences: for sentence in beam_sentence: assert sentence in {"goodbye", "hello", "i like to read", "i am hungry"}
def plot_batch(path, model, dl, fnames, n=5): """ Plot first n batch predictions path : data path model : model dl: dataloader fnames: filenames like path/fnames[i] """ for i, (*x, y) in enumerate(dl): bs = len(y) out = model(Variable(*x)) out_np = to_np(out) batch_fnames = fnames[bs * i:bs * (i + 1)] for fname, out in zip(batch_fnames, out_np): im = open_image(path / fname) mask = (sigmoid(out.squeeze(0)) > 0.5).astype("uint8") mask = cv2.resize(mask, (768, 768)) show_img_masks(im, mask) plt.close() if i + 1 == n: break
def bleu_score(preds, targs, stoi=None): sf = SmoothingFunction().method1 preds = torch.max(preds, dim=-1)[1][:-1] bleus = np.zeros(targs.size(1)) for res in zip(to_np(targs, preds)): if len(res[1]) > 2: bleu = sentence_bleu([res[1]], res[2], smoothing_function=sf, weights=(1 / 3., 1 / 3., 1 / 3.)) elif len(res[1]) == 2: bleu = sentence_bleu([res[1]], res[2], smoothing_function=sf, weights=(0.5, 0.5)) else: bleu = sentence_bleu([res[1]], res[2], smoothing_function=sf, weights=(1.0, )) bleus.append(bleu) return
def custom_validate(cache: Cache, text_field: Field, use_subword_aware_metrics: bool, stepper, dl, metrics, epoch, seq_first, validate_skip): if use_subword_aware_metrics: raise ValueError( "use_subword_aware_metrics=True is curreently not implemented") if cache: logger.info( f"Using neural cache with theta: {cache.theta}, lambdah: {cache.lambdah}, window: {cache.window}." ) else: logger.info(f"Not using neural cache!") avg_batch_losses = [] avg_batch_accuracies = [] avg_batch_accuracies_strict = [] actual_probs, pred_vals = [], [] n_words_list = [] stepper.reset(False) full_word_iterators = None with no_grad_context(): next_word_history = None pointer_history = None n_iter = len(dl) for i, (*x, flattened_targets) in enumerate(iter(dl)): logger.info(f'Iteration {i} out of {n_iter}') x = VV(x) flattened_targets = VV(flattened_targets) flattened_preds, all_layers_hidden_activations, _ = stepper.evaluate_with_cache( x, flattened_targets) batch_size = x[0].size(1) vocab_size = flattened_preds.size(1) flattened_preds_softmax = torch.nn.functional.softmax( flattened_preds, dim=-1) del flattened_preds if cache: start_idx = len( next_word_history) if next_word_history is not None else 0 targets = flattened_targets.view(-1, batch_size) # bptt x bs one_hot_encoded_targets = torch.stack([ torch.cat([ one_hot(target.data[0], vocab_size) for target in i_target_in_batch ], dim=0) for i_target_in_batch in targets ]) # bptt x bs x vocab_size [290x32x5000x4=185MB] next_word_history = one_hot_encoded_targets if next_word_history is None \ else torch.cat([next_word_history, one_hot_encoded_targets]) last_hidden_layer_activations = all_layers_hidden_activations[ -1] # bptt x batch size x layer size last_history_var = VV(last_hidden_layer_activations.data) pointer_history = last_history_var if pointer_history is None \ else torch.cat([pointer_history, last_history_var], dim=0) # at most window x bs x layer_size preds_softmax = flattened_preds_softmax.view( -1, batch_size, vocab_size) # bptt x bs x vocab_size predictions = cache_calc( preds_softmax=preds_softmax, start_idx=start_idx, next_word_history=VV(next_word_history), pointer_history=pointer_history, last_hidden_layer_activations=last_hidden_layer_activations, targets=targets, cache=cache, text_field=text_field) next_word_history = next_word_history[-cache.window:] pointer_history = pointer_history[-cache.window:] flattened_predictions = predictions.view(-1, vocab_size) else: flattened_predictions = flattened_preds_softmax actual_probs = torch.gather(flattened_predictions, -1, flattened_targets.view(-1, 1)) if use_subword_aware_metrics: pred_vals = torch.max(flattened_predictions, dim=-1)[1] cross_entropy_loss, accuracy, accuracy_strict, n_words = calc_subword_aware_metrics( pred_vals, actual_probs) else: n_words = flattened_targets.size(0) cross_entropy_loss = -torch.log(actual_probs).sum() / n_words accuracy = fastai.metrics.accuracy(flattened_predictions.data, flattened_targets) accuracy_strict = mrr(flattened_predictions.data, flattened_targets) avg_batch_losses.append(to_np(cross_entropy_loss)) avg_batch_accuracies.append(accuracy) avg_batch_accuracies_strict.append(accuracy_strict) n_words_list.append(n_words) return [ np.average(avg_batch_losses, 0, weights=n_words_list), np.average(avg_batch_accuracies, 0, weights=n_words_list), np.average(avg_batch_accuracies_strict, 0, weights=n_words_list) ]
sz = 224 bs = 128 model = resnet34 trn_tfms, val_tfms = tfms_from_model(model, sz) preprocess = val_tfms model = ConvnetBuilder(model, 2, False, False, pretrained=False).model sd = torch.load(model_weight_path) model.load_state_dict(sd) model.eval(); if __name__ == "__main__": img_pth = sys.argv[1] output_file = sys.argv[2] img = load_img(img_pth) preprocessed_img = preprocess(img) preprocessed_img = V(preprocessed_img).unsqueeze(0) logprob = model(preprocessed_img) logprob = to_np(logprob) prob = np.exp(logprob) prob = prob[0] with open(output_file, 'w') as f: f.write(str(prob[1]))
def test_forward(self): x = core.V(np.array([[1., 2.]]), requires_grad=False) output = core.to_np(self.simple_net.forward(x)) np.testing.assert_almost_equal(output, [[-1.435481, -0.27181]], decimal=4)
x = Variable(T(x), requires_grad=False, volatile=True) # if FEEDBACK: # output = learner.model(x, None) # FEEDBACK # else: # output = learner.model(x) # shape (1, 14) # output_np = to_np(output[0]) # CNN and RNN model output_rnn = learner_rnn.model(x) # shape (1, 14) output_cnn = learner_cnn.model(x) # # just angle from cnn # output_np = to_np(output_rnn[0]) # output_np[0] = to_np(output_cnn[0])[0] # angle # # all except distances from cnn output_np = to_np(output_cnn[0]) output_np[4] = to_np(output_rnn[0])[4] # d_L output_np[5] = to_np(output_rnn[0])[5] # d_R output_np[10] = to_np(output_rnn[0])[10] # d_LL output_np[11] = to_np(output_rnn[0])[11] # d_MM output_np[12] = to_np(output_rnn[0])[12] # d_RR pred_indicators = transform_range_output(output_np, UNIT_RANGES, INDIC_RANGES) print("network raw output", output_np) print("pred_indicators", pred_indicators) indicators_formatted = inds_net_to_cntrl(pred_indicators) print("indicators_formatted", indicators_formatted) ground_truth = drive.read_indicators()
class AttentionDataset(Dataset): def __init__(self): df = pd.read_csv('../csv/Data_Entry_Clean.csv') self.image_names = df['Image Index'].values self.tfm = transforms.Compose([ transforms.Resize(224), transforms.ToTensor(), transforms.Normalize(IMAGENET_MEAN, IMAGENET_STD) ]) def __getitem__(self, i): image = Image.open(PATH/IMAGE_DN/self.image_names[i]).convert('RGB') return self.tfm(image), self.image_names[i] def __len__(self): return len(self.image_names) dataset = AttentionDataset() dataloader = DataLoader(dataset, 16) # fastai dataloader g = HeatmapGenerator() for images, image_names in tqdm(iter(dataloader)): print() heatmaps, _ = g.generate(images) for i in range(16): heatmap = to_np(heatmaps[i]) heatmap = cv2.resize(heatmap, (1024, 1024)) crop_attention(heatmap, image_names[i])
def run(constant_overwrites): dir_path = os.path.dirname(os.path.realpath(__file__)) config_path = os.path.join(dir_path, 'hyperparams.yml') constants = merge_dict(load_hyperparams(config_path), constant_overwrites) # data subdir expected to exist LM_PATH.mkdir(exist_ok=True) (LM_PATH / 'tmp').mkdir(exist_ok=True) CLAS_PATH.mkdir(exist_ok=True) (CLAS_PATH / 'tmp').mkdir(exist_ok=True) data_path = dir_path + '/train.csv' if not os.path.exists(data_path): train_df, val_df, test_df, x_train, y_train, x_val, y_val, x_test, y_test, classes = preprocess_csv( ) else: train_df = pd.read_csv(dir_path + '/train.csv', header=None, chunksize=CHUNKSIZE) # x_train, y_train = train_df[0].values, train_df[1].values val_df = pd.read_csv(dir_path + '/val.csv', header=None, chunksize=CHUNKSIZE) # x_val, y_val = val_df[0].values, val_df[1].values # test_df = pd.read_csv(dir_path + '/test.csv', header=None, chunksize=CHUNKSIZE) # x_test, y_test = test_df[0].values, test_df[1].values # classes = np.genfromtxt(dir_path + '/classes.txt', dtype=str) # print('Counts x_train: {}, y_train: {}, x_val: {}, y_val: {}, x_test: {}, y_test: {}, classes: {}' # .format(len(x_train), len(y_train), len(x_val), len(y_val), len(x_test), len(y_test), len(classes))) if constants['train_lm']: logging.info('Training LM...') if (LM_PATH / 'tmp' / 'tok_train.npy').exists(): logging.info('Loading tokens...') tok_train = np.load(LM_PATH / 'tmp' / 'tok_train.npy') tok_val = np.load(LM_PATH / 'tmp' / 'tok_val.npy') else: logging.info('Get tokens...') tok_train, labels_train = get_all(train_df, 1) tok_val, labels_val = get_all(val_df, 1) np.save(LM_PATH / 'tmp' / 'tok_train.npy', tok_train) np.save(LM_PATH / 'tmp' / 'tok_val.npy', tok_val) if (LM_PATH / 'tmp' / 'itos.pkl').exists(): train_ids = np.load(LM_PATH / 'tmp' / 'train_ids.npy') val_ids = np.load(LM_PATH / 'tmp' / 'val_ids.npy') itos = pickle.load(open(LM_PATH / 'tmp' / 'itos.pkl', 'rb')) else: freq = collections.Counter(t for ts in tok_train for t in ts) itos = [t for t, k in freq.most_common(MAX_VOCAB) if k > MIN_FREQ] # int idx to str token itos.insert(0, '_pad_') itos.insert(1, '_unk_') stoi = collections.defaultdict( lambda: 0, {t: i for i, t in enumerate(itos)}) # str token to int idx train_ids = np.array([[stoi[t] for t in ts] for ts in tok_train]) val_ids = np.array([[stoi[t] for t in ts] for ts in tok_val]) np.save(LM_PATH / 'tmp' / 'train_ids.npy', train_ids) np.save(LM_PATH / 'tmp' / 'val_ids.npy', val_ids) pickle.dump(itos, open(LM_PATH / 'tmp' / 'itos.pkl', 'wb')) vocab_size = len(itos) emb_dim, n_hidden, n_layers = 400, 1150, 3 pre_path = PATH / 'models' / 'wt103' pre_lm_path = pre_path / 'fwd_wt103.h5' w = torch.load(pre_lm_path, map_location=lambda storage, loc: storage) enc_w = fastai.to_np(w['0.encoder.weight']) row_mean = enc_w.mean(0) itos_model = pickle.load((pre_path / 'itos_wt103.pkl').open('rb')) stoi_model = collections.defaultdict( lambda: -1, {t: i for i, t in enumerate(itos_model)}) new_w = np.zeros((vocab_size, emb_dim), dtype=np.float32) for i, t in enumerate(itos): j = stoi_model[t] new_w[i] = enc_w[j] if j >= 0 else row_mean w['0.encoder.weight'] = fastai.T(new_w) w['0.encoder_with_dropout.embed.weight'] = fastai.T(np.copy(new_w)) w['1.decoder.weight'] = fastai.T(np.copy(new_w)) wd = 1e-7 # weight decay bptt = 70 # backpropagation through time, a.k.a. ngrams batch_size = 52 optimizer_fn = functools.partial(torch.optim.Adam, betas=(0.8, 0.99)) dl_train = ftext.LanguageModelLoader(np.concatenate(train_ids), batch_size, bptt) # data loader dl_val = ftext.LanguageModelLoader(np.concatenate(val_ids), batch_size, bptt) md = ftext.LanguageModelData(PATH, 1, vocab_size, dl_train, dl_val, batch_size=batch_size, bptt=bptt) drops = np.array([0.25, 0.1, 0.2, 0.02, 0.15]) * 0.7 learner = md.get_model(optimizer_fn, emb_dim, n_hidden, n_layers, dropouti=drops[0], dropout=drops[1], wdrop=drops[2], dropoute=drops[3], dropouth=drops[4]) learner.metrics = [fmetrics.accuracy] learner.freeze_to(-1) learner.model.load_state_dict(w) lr = 1e-3 lrs = lr learner.fit(lrs / 2, 1, wds=wd, use_clr=(32, 2), cycle_len=1) learner.save('lm_last_ft') learner.lr_find(start_lr=lrs / 10, end_lr=lrs * 10, linear=True) learner.sched.plot() learner.fit(lrs, 1, wds=wd, use_clr=(20, 10), cycle_len=15) learner.save('lm1') learner.save_encoder('lm1_enc') learner.sched.plot_loss() if (CLAS_PATH / 'tmp' / 'tok_train.npy').exists(): tok_train = np.load(CLAS_PATH / 'tmp' / 'tok_train.npy') tok_val = np.load(CLAS_PATH / 'tmp' / 'tok_val.npy') labels_train = np.load(CLAS_PATH / 'tmp' / 'labels_train.npy') labels_val = np.load(CLAS_PATH / 'tmp' / 'labels_val.npy') else: tok_train, labels_train = get_all(train_df, 1) tok_val, labels_val = get_all(val_df, 1) np.save(CLAS_PATH / 'tmp' / 'tok_train.npy', tok_train) np.save(CLAS_PATH / 'tmp' / 'tok_val.npy', tok_val) np.save(CLAS_PATH / 'tmp' / 'labels_train.npy', labels_train) np.save(CLAS_PATH / 'tmp' / 'labels_val.npy', labels_val) if (CLAS_PATH / 'tmp' / 'itos.pkl').exists(): train_ids = np.load(CLAS_PATH / 'tmp' / 'train_ids.npy') val_ids = np.load(CLAS_PATH / 'tmp' / 'val_ids.npy') itos = pickle.load(open(CLAS_PATH / 'tmp' / 'itos.pkl', 'rb')) else: freq = collections.Counter(t for ts in tok_train for t in ts) itos = [t for t, k in freq.most_common(MAX_VOCAB) if k > MIN_FREQ] # int idx to str token itos.insert(0, '_pad_') itos.insert(1, '_unk_') stoi = collections.defaultdict( lambda: 0, {t: i for i, t in enumerate(itos)}) # str token to int idx train_ids = np.array([[stoi[t] for t in ts] for ts in tok_train]) val_ids = np.array([[stoi[t] for t in ts] for ts in tok_val]) np.save(CLAS_PATH / 'tmp' / 'train_ids.npy', train_ids) np.save(CLAS_PATH / 'tmp' / 'val_ids.npy', val_ids) pickle.dump(itos, open(CLAS_PATH / 'tmp' / 'itos.pkl', 'wb')) vocab_size = len(itos) bptt = 70 # backpropagation through time, a.k.a. ngrams emb_dim, n_hidden, n_layers = 400, 1150, 3 # optimizer_fn = functools.partial(optim.Adam, betas=(0.8, 0.99)) batch_size = 48 min_label = min(labels_train) labels_train -= min_label labels_val -= min_label k = int(max(labels_train)) + 1 ds_train = ftext.TextDataset(train_ids, labels_train) ds_val = ftext.TextDataset(val_ids, labels_val) sampler_train = ftext.SortishSampler(train_ids, key=lambda x: len(train_ids[x]), bs=batch_size // 2) sampler_val = ftext.SortSampler(val_ids, key=lambda x: len(val_ids[x])) dl_train = dataloader.DataLoader(ds_train, batch_size // 2, transpose=True, num_workers=1, pad_idx=1, sampler=sampler_train) dl_val = dataloader.DataLoader(ds_val, batch_size // 2, transpose=True, num_workers=1, pad_idx=1, sampler=sampler_val) md = fdata.ModelData(PATH, dl_train, dl_val) # drops = np.array([0.4, 0.5, 0.05, 0.3, 0.1]) drops = np.array([0.4, 0.5, 0.05, 0.3, 0.4]) * 0.5 model = lm_rnn.get_rnn_classifier(bptt, 20 * 70, k, vocab_size, emb_sz=emb_dim, n_hid=n_hidden, n_layers=n_layers, pad_token=1, layers=[emb_dim * 3, 50, k], drops=[drops[4], 0.1], dropouti=drops[0], wdrop=drops[1], dropoute=drops[2], dropouth=drops[3]) optimizer_fn = functools.partial(torch.optim.Adam, betas=(0.7, 0.99)) # learner = RNN_Learner(md, TextModel(to_gpu(model)), opt_fn=optimizer_fn) learner = ftext.RNN_Learner(md, ftext.TextModel(model), opt_fn=optimizer_fn) learner.reg_fn = functools.partial(lm_rnn.seq2seq_reg, alpha=2, beta=1) learner.clip = 25.0 learner.metrics = [fmetrics.accuracy] # lr = 3e-3 # lrm = 2.6 # lrs = np.array([lr / lrm**4, lr / lrm**3, lr / lrm**2, lr / lrm, lr]) lrs = np.array([1e-4, 1e-4, 1e-4, 1e-3, 1e-2]) # wd = 1e-7 # weight decay wd = 0 learner.load_encoder('lm1_enc') learner.freeze_to(-1) learner.lr_find(lrs / 1000) learner.sched.plot() learner.fit(lrs, 1, wds=wd, cycle_len=1, use_clr=(8, 3)) learner.save('clas_0')
print("Controlling: ", drive.is_controlling()) input("Press key to start...") while True: if drive.is_written(): print("Reading img") img = drive.read_image() # HWC, BGR img_np = np.asarray(img).reshape(HEIGHT, WIDTH, 3)[:,:,::-1] #.astype('uint8') # HWC, RGB # import pdb; pdb.set_trace() #plt.imshow(img_np) #plt.show() img_np = (img_np/255).astype('float32') x = trn_tfms(img_np)[np.newaxis, ...] # shape (210, 280, 3) -> (3, 210, 210) -> (1, 3, 210, 210) x = Variable(T(x),requires_grad=False, volatile=True) output = learner.model(x) # shape (1, 14) pred_indicators = transform_range_output(to_np(output[0]), UNIT_RANGES, INDIC_RANGES) print("network raw output", output) print("pred_indicators", pred_indicators) indicators_formatted = format_indicators(pred_indicators) print("indicators_formatted", indicators_formatted) ground_truth = drive.read_indicators() print("ground_truth", ground_truth) drive.controller(indicators_formatted) drive.update_visualizations(indicators_formatted, ground_truth) drive.write(False) # Shared data read, and TORCS may continue drive.wait_key(1)
def test_BiRNNEncoder(): ntoken = 4 emb_sz = 2 nhid = 6 # Given a birnnencoder encoder = EmbeddingRNNEncoder(ntoken, emb_sz, nhid=nhid, nlayers=2, pad_token=0, dropouth=0.0, dropouti=0.0, dropoute=0.0, wdrop=0.0) encoder = to_gpu(encoder) assert encoder is not None weight = encoder.encoder.weight assert (4, 2) == weight.shape sl = 2 bs = 3 np.random.seed(0) inputs = np.random.randint(0, ntoken, sl * bs).reshape(sl, bs) vin = V(T(inputs)) # Then the initial output states should be zero encoder.reset(bs) initial_hidden = encoder.hidden h = [] c = [] for layer in initial_hidden: h.append(layer[0].data.cpu().numpy()) c.append(layer[1].data.cpu().numpy()) assert h[-1].sum() == 0 assert c[-1].sum() == 0 embeddings = encoder.encoder(vin) assert (2, 3, emb_sz) == embeddings.shape # Then the the new states are different from before raw_outputs, outputs = encoder(vin) for r, o in zip(raw_outputs, outputs): assert np.allclose(to_np(r), to_np(o)) initial_hidden = encoder.hidden h1 = [] c1 = [] for hl, cl, layer in zip(h, c, initial_hidden): h1.append(to_np(layer[0])) c1.append(to_np(layer[0])) assert ~np.allclose(hl, h1[-1]) assert ~np.allclose(cl, c1[-1]) # Then the the new states are different from before raw_outputs, outputs = encoder(vin) for r, o in zip(raw_outputs, outputs): assert np.allclose(to_np(r), to_np(o)) initial_hidden = encoder.hidden for hl, cl, layer in zip(h1, c1, initial_hidden): h_new = to_np(layer[0]) c_new = to_np(layer[0]) assert ~np.allclose(hl, h_new) assert ~np.allclose(cl, c_new)