示例#1
0
def test_from_ids_works_for_equally_length_sentences():
    this_tests(TextClasDataBunch.from_ids)
    ids = [np.array([0])]*10
    lbl = [0]*10
    data = TextClasDataBunch.from_ids('/tmp', vocab=Vocab({0: BOS, 1:PAD}),
                                      train_ids=ids, train_lbls=lbl,
                                      valid_ids=ids, valid_lbls=lbl, classes={0:0}, bs=8)
示例#2
0
def test_from_csv_and_from_df():
    this_tests(TextClasDataBunch.from_df, TextClasDataBunch.from_csv)
    path = untar_data(URLs.IMDB_SAMPLE)
    df = text_df(['neg','pos']) #"fast ai is a cool project", "hello world"
    trn_df,val_df,tst_df = df.iloc[:20],df.iloc[20:],df.iloc[:10]
    data1 = TextClasDataBunch.from_df(path, train_df=trn_df, valid_df=val_df, test_df=tst_df, label_cols=0,
                                      text_cols=["text"], no_check=True)
    assert len(data1.classes) == 2
    x,y = next(iter(data1.valid_dl)) # Will fail if the SortSampler keys get messed up between train and valid.
    df = text_df(['neg','pos','neg pos'])
    data2 = TextClasDataBunch.from_df(path, train_df=trn_df, valid_df=val_df,
                                  label_cols=0, text_cols=["text"], label_delim=' ',
                                  tokenizer=Tokenizer(pre_rules=[special_fastai_test_rule]), no_check=True)
    assert len(data2.classes) == 2
    x,y = data2.train_ds[0]
    assert len(y.data) == 2
    assert '@fastdotai' in data2.train_ds.vocab.itos,  "custom tokenzier not used by TextClasDataBunch"
    text_csv_file(path/'tmp.csv', ['neg','pos'])
    data3 = TextLMDataBunch.from_csv(path, 'tmp.csv', test='tmp.csv', label_cols=0, text_cols=["text"], bs=2)
    assert isinstance(data3.train_ds.y[0], EmptyLabel)
    data4 = TextLMDataBunch.from_csv(path, 'tmp.csv', label_cols=0, text_cols=["text"], max_vocab=5, bs=2)
    assert 5 <= len(data4.train_ds.vocab.itos) <= 5+8 # +(8 special tokens - UNK/BOS/etc)
    data4.batch_size = 8

    os.remove(path/'tmp.csv')
示例#3
0
def test_from_ids_works_for_variable_length_sentences():
    this_tests(TextClasDataBunch.from_ids)
    ids = [np.array([0]),np.array([0,1])]*5 # notice diffrent number of elements in arrays
    lbl = [0]*10
    data = TextClasDataBunch.from_ids('/tmp', vocab=Vocab({0: BOS, 1:PAD}),
                                      train_ids=ids, train_lbls=lbl,
                                      valid_ids=ids, valid_lbls=lbl, classes={0:0}, bs=8)
def test_vision_pil2tensor():
    this_tests(pil2tensor)
    path  = Path(__file__).parent / "data/test/images"
    files = list(Path(path).glob("**/*.*"))
    pil_passed, pil_failed = [],[]
    for f in files:
        try:
            im = PIL.Image.open(f)
            #provoke read of the file so we can isolate PIL issue separately
            b = np.asarray(im.convert("RGB"))
            pil_passed.append(f)
        except:
            pil_failed.append(f)

    pil2tensor_passed,pil2tensor_failed = [],[]
    for f in pil_passed:
        try :
            # it doesn't matter for the test if we convert "RGB" or "I"
            im = PIL.Image.open(f).convert("RGB")
            t  = pil2tensor(im,np.float)
            pil2tensor_passed.append(f)
        except:
            pil2tensor_failed.append(f)
            print(f"converting file: {f}  had Unexpected error:", sys.exc_info()[0])

    if len(pil2tensor_failed)>0 :
        print("\npil2tensor failed to convert the following images:")
        [print(f) for f in pil2tensor_failed]

    assert(len(pil2tensor_passed) == len(pil_passed))
示例#5
0
def test_trange_of():
    this_tests(trange_of)
    t = trange_of(a)
    assert len(t) == len(a)
    assert t[0] == 0
    assert t[1] == 1
    assert t[2] == 2
def test_destroy():
    msg = "this object has been destroyed"
    learn = fake_learner()
    this_tests(learn.destroy)
    with CaptureStdout() as cs: learn.destroy()
    assert "this Learner object self-destroyed" in cs.out

    # should be able to re-run learn.destroy multiple times for nb convenience
    with CaptureStdout() as cs: learn.destroy()
    assert msg in cs.out

    # should be able to run normal methods, except they are no-ops and say that they are
    with CaptureStdout() as cs: learn.fit(1)
    assert msg in cs.out

    # should be able to call attributes, except they are gone and say so
    # unless they are __getattr__' loaded from Learner, in which case they are still normal
    for attr in ['data', 'model', 'callbacks']:
        with CaptureStdout() as cs: val = getattr(learn, attr, None)
        assert msg in cs.out, attr
        assert val is None, attr

    # check that `destroy` didn't break the Learner class
    learn = fake_learner()
    with CaptureStdout() as cs: learn.fit(1)
    assert "epoch" in cs.out
    assert "train_loss" in cs.out
def test_image_resize_same_size_shortcut():
    this_tests(Image.resize)
    px = torch.Tensor([[[1, 2,], [3, 4]]])
    image = Image(px)
    old_size = image.size
    image = image.resize(px.size()) 
    assert(image is not None and (old_size == image.size))
示例#8
0
def test_DataBunch_onebatch():
    data = fake_data(n_in=4, n_out=5, batch_size=6)
    this_tests(data.one_batch)
    x,y = data.one_batch()
    assert 4 == x[0].shape[0]
    assert 6 == x.shape[0]
    assert 6 == y.shape[0]
示例#9
0
def test_DataBunch_show_batch(capsys):
    data = fake_data()
    this_tests(data.show_batch)
    data.show_batch()
    captured = capsys.readouterr()
    match = re.findall(r'tensor', captured.out)
    assert match
示例#10
0
def test_check_perf(capsys):
    this_tests(check_perf)
    check_perf()
    captured = capsys.readouterr()
    #print(captured.out)
    match = re.findall(rf'Running Pillow.*?{Image.PILLOW_VERSION}', captured.out)
    assert match
示例#11
0
def test_forget_mult_cuda():
    this_tests(ForgetMultGPU, BwdForgetMultGPU)
    x,f = torch.randn(5,3,20).cuda().chunk(2, dim=2)
    x,f = x.contiguous().requires_grad_(True),f.contiguous().requires_grad_(True)
    th_x,th_f = detach_and_clone(x),detach_and_clone(f)
    for (bf, bw) in [(True,True), (False,True), (True,False), (False,False)]:
        forget_mult = BwdForgetMultGPU if bw else ForgetMultGPU
        th_out = forget_mult_CPU(th_x, th_f, hidden_init=None, batch_first=bf, backward=bw)
        th_loss = th_out.pow(2).mean()
        th_loss.backward()
        out = forget_mult.apply(x, f, None, bf)
        loss = out.pow(2).mean()
        loss.backward()
        assert torch.allclose(th_out,out, rtol=1e-4, atol=1e-5)
        assert torch.allclose(th_x.grad,x.grad, rtol=1e-4, atol=1e-5)
        assert torch.allclose(th_f.grad,f.grad, rtol=1e-4, atol=1e-5)
        for p in [x,f, th_x, th_f]:
            p = p.detach()
            p.grad = None
        h = torch.randn((5 if bf else 3), 10).cuda().requires_grad_(True)
        th_h = detach_and_clone(h)
        th_out = forget_mult_CPU(th_x, th_f, hidden_init=th_h, batch_first=bf, backward=bw)
        th_loss = th_out.pow(2).mean()
        th_loss.backward()
        out = forget_mult.apply(x.contiguous(), f.contiguous(), h, bf)
        loss = out.pow(2).mean()
        loss.backward()
        assert torch.allclose(th_out,out, rtol=1e-4, atol=1e-5)
        assert torch.allclose(th_x.grad,x.grad, rtol=1e-4, atol=1e-5)
        assert torch.allclose(th_f.grad,f.grad, rtol=1e-4, atol=1e-5)
        assert torch.allclose(th_h.grad,h.grad, rtol=1e-4, atol=1e-5)
        for p in [x,f, th_x, th_f]:
            p = p.detach()
            p.grad = None
示例#12
0
def test_callbacks_learner(data, model):
    this_tests(Callback)

    # single callback in learner constructor
    learn = Learner(data, model, metrics=accuracy, callback_fns=DummyCallback)
    with CaptureStdout() as cs: learn.fit_one_cycle(2)
    check_dummy_metric(cs.out)

    # list of callbacks in learner constructor
    learn = Learner(data, model, metrics=accuracy, callback_fns=[DummyCallback])
    with CaptureStdout() as cs: learn.fit_one_cycle(2)
    check_dummy_metric(cs.out)

    # single callback append
    learn = Learner(data, model, metrics=accuracy)
    learn.callbacks.append(DummyCallback(learn))
    with CaptureStdout() as cs: learn.fit_one_cycle(2)
    check_dummy_metric(cs.out)

    # list of callbacks append: python's append, so append([x]) will not do the right
    # thing, so it's expected to fail
    learn = Learner(data, model, metrics=[accuracy])
    learn.callbacks.append([DummyCallback(learn)])
    error = ''
    try:
        with CaptureStdout() as cs: learn.fit_one_cycle(2)
    except Exception as e:
        error = str(e)
    error_pat = "'list' object has no attribute 'on_train_begin'"
    assert error_pat in error, f"{error_pat} is in the exception:\n{error}"
示例#13
0
def test_image_resize(path, path_var_size):
    this_tests(ImageDataBunch.from_name_re)
    # in this test the 2 datasets are:
    # (1) 28x28,
    # (2) var-size but larger than 28x28,
    # and the resizes are always less than 28x28, so it always tests a real resize
    for p in [path, path_var_size]: # identical + var sized inputs
        fnames = get_image_files(p/'train', recurse=True)
        pat = r'/([^/]+)\/\d+.png$'
        for size in [14, (14,14), (14,20)]:
            for rm_name in rms:
                rm = getattr(ResizeMethod, rm_name)
                args = f"path={p}, size={size}, resize_method={rm_name}"

                # resize the factory method way
                with CaptureStderr() as cs:
                    data = ImageDataBunch.from_name_re(p, fnames, pat, ds_tfms=None, size=size, resize_method=rm)
                assert len(cs.err)==0, f"[{args}]: got collate_fn warning {cs.err}"
                check_resized(data, size, args)

                # resize the data block way
                with CaptureStderr() as cs:
                    data = (ImageList.from_folder(p)
                            .split_none()
                            .label_from_folder()
                            .transform(size=size, resize_method=rm)
                            .databunch(bs=2)
                            )
                assert len(cs.err)==0, f"[{args}]: got collate_fn warning {cs.err}"
                check_resized(data, size, args)
示例#14
0
def test_from_csv_and_from_df(path):
    this_tests(ImageDataBunch.from_csv, ImageDataBunch.from_df)
    for func in ['from_csv', 'from_df']:
        files = []
        if func is 'from_df': data = ImageDataBunch.from_df(path, df=pd.read_csv(path/'labels.csv'), size=28)
        else: data = ImageDataBunch.from_csv(path, size=28)
        mnist_tiny_sanity_test(data)
示例#15
0
def test_purge():
    learn = fake_learner() # don't use fixture - we mess with the object
    this_tests(learn.purge)

    # just testing we can run each of these
    learn.purge()
    learn.purge(clear_opt=False)

    # writable dir
    model_dir_orig = learn.model_dir
    learn.model_dir = "." # should succeed
    learn.purge()
    learn.model_dir = model_dir_orig

    # should fail to purge with a non-existent path
    learn.model_dir = "read_only"
    os.makedirs(learn.path/"read_only")
    os.chmod(learn.path/"read_only", mode=444)
    
    try: learn.purge()
    except Exception as e:
        assert "Can't write to" in str(e) # should fail
    else: assert False, "should have failed with non-writable path"

    finally: # restore the learner fixture
        learn.model_dir = model_dir_orig
        shutil.rmtree(learn.path/"read_only")
示例#16
0
def test_fit(learn):
    this_tests(learn.fit)
    # Test confirms learning rate and momentum are stable, see difference to test_fit_one_cycle
    learning_rate, weight_decay, eps = 3e-3, 1e-2,  4
    with CaptureStdout() as cs:  learn.fit(epochs=eps, lr=learning_rate, wd=weight_decay)
    assert set(learn.recorder.lrs) == {learning_rate}
    assert set(learn.recorder.moms) == {learn.recorder.moms[0]}
示例#17
0
def test_model_load_mem_leak():
    "testing memory leak on load"
    pytest.xfail("memory leak in learn.load()")

    path = untar_data(URLs.MNIST_TINY)
    data = ImageDataBunch.from_folder(path, ds_tfms=([], []), bs=2)
    learn = cnn_learner(data, models.resnet18, metrics=accuracy)
    this_tests(learn.load)
    gpu_mem_reclaim() # baseline
    used_before = gpu_mem_get_used()

    name = 'mnist-tiny-test-load-mem-leak'
    model_path = learn.save(name, return_path=True)
    _ = learn.load(name)
    if os.path.exists(model_path): os.remove(model_path)
    used_after = gpu_mem_get_used()

    # models.resnet18 loaded in GPU RAM is about 50MB
    # calling learn.load() of a saved and then instantly re-loaded model shouldn't require more GPU RAM
    # XXX: currently w/o running gc.collect() this temporarily leaks memory and causes fragmentation - the fragmentation can't be tested from here, but it'll get automatically fixed once load is fixed. load() must unload first the previous model, gc.collect() and only then load the new one onto cuda.
    assert isclose(used_before, used_after, abs_tol=6), f"load() and used GPU RAM: before load(): {used_before}, after: {used_after}"

    # this shows how it should have been
    gc.collect()
    gpu_cache_clear()
    used_after_reclaimed = gpu_mem_get_used()
    # XXX: not sure where 6MB get lost still but for now it's a small leak - need to test with a bigger model
    assert isclose(used_before, used_after_reclaimed, abs_tol=6),f"load() and used GPU RAM: before load(): {used_before}, after: {used_after}, after gc.collect() {used_after_reclaimed} used"
def test_crop_without_size():
    this_tests(crop)
    path = untar_data(URLs.MNIST_TINY)/'train'/'3'
    files = get_image_files(path)
    img = open_image(path/files[0])
    tfms = get_transforms()
    img = img.apply_tfms(tfms[0])
示例#19
0
def test_save_load(learn):
    this_tests(learn.save, learn.load, learn.purge)
    name = 'mnist-tiny-test-save-load'
    train_items_before = len(learn.data.train_ds.items)
    model_summary_before = learn.summary()

    # testing that all these various sequences don't break each other
    model_path = learn.save(name, return_path=True)
    _ = learn.load(name, purge=True)
    learn.data.sanity_check()
    check_learner(learn, model_summary_before, train_items_before)

    learn.purge()
    _ = learn.load(name)
    _ = learn.load(name)
    model_path = learn.save(name, return_path=True)
    _ = learn.load(name, purge=True)
    check_learner(learn, model_summary_before, train_items_before)

    # Test save/load using bytes streams
    output_buffer = io.BytesIO()
    learn.save(output_buffer)
    learn.purge()
    input_buffer = io.BytesIO(output_buffer.getvalue())
    _ = learn.load(input_buffer)
    check_learner(learn, model_summary_before, train_items_before)

    # cleanup
    if os.path.exists(model_path): os.remove(model_path)
示例#20
0
def test_multi_category():
    this_tests('na')
    c1 = [1,3,2,3,1]
    c2     = ['c a', 'a b', 'b c', '', 'a']
    c2_exp = ['c;a', 'a;b', 'b;c', '', 'a']
    c2_obj = [['c', 'a'], ['a', 'b'], ['b', 'c'], [], ['a']]
    df = pd.DataFrame(dict(c1=c1,c2=c2))
    trn_idx = [0,1,3]

    l1 = ItemList.from_df(df, cols=0)
    sd = l1.split_by_idx([2,4])

    ll = sd.label_from_df(1, label_delim=' ')
    x,y = ll.train.x,ll.train.y
    c2i = {v:k for k,v in enumerate(ll.train.classes)}

    chk(x.items, array(c1)[trn_idx])
    chk(ll.train.classes, ll.valid.classes)
    assert set(ll.train.classes)==set(list('abc'))
    chk(list(map(str, y)), array(c2_exp)[trn_idx])
    chk([o.obj for o in y], array(c2_obj)[trn_idx])
    exp = [[c2i[p] for p in o] for o in array(c2_obj)[trn_idx]]
    chk([o.raw for o in y], exp)
    t = c2_obj[1]
    exp = [0.,0.,0.]
    exp[c2i[t[0]]] = 1.
    exp[c2i[t[1]]] = 1.
    chk(y[1].data, exp)
示例#21
0
def test_gan_trainer(gan_learner):
    this_tests(GANTrainer)
    gan_trainer = gan_learner.gan_trainer
    with CaptureStdout() as cs: gan_learner.fit(1, 1e-4)
    assert gan_trainer.imgs
    assert gan_trainer.gen_mode
    assert gan_trainer.titles
示例#22
0
def test_clean_tear_down(path):
    this_tests('na')
    docstr = "test DataLoader iter doesn't get stuck"
    data = ImageDataBunch.from_folder(path, ds_tfms=(rand_pad(2, 28), []))
    data.normalize()
    data = ImageDataBunch.from_folder(path, ds_tfms=(rand_pad(2, 28), []))
    data.normalize()
示例#23
0
def test_model2half():
    this_tests(model2half)
    m = simple_cnn([3,6,6],bn=True)
    m = model2half(m)
    conv1 = m[0][0]
    bn = m[0][2]
    assert isinstance(conv1.weight, torch.HalfTensor)
    assert isinstance(bn.weight, torch.FloatTensor)
def test_submodule_name():
    this_tests(nbtest._submodule_name)
    result:str = nbtest._submodule_name(nbtest.doctest)
    assert result == 'gen_doc', 'should return submodule'

    from fastai.core import ifnone
    result:str = nbtest._submodule_name(ifnone)
    assert result == None, f'fastai/module should not have a submodule: {result}'
示例#25
0
def test_splitdata_datasets():
    c1,ratio,n = list('abc'),0.2,10

    this_tests(ItemList.split_by_rand_pct)
    sd = ItemList(range(n)).split_by_rand_pct(ratio).label_const(0)
    assert len(sd.train)==(1-ratio)*n, 'Training set is right size'
    assert len(sd.valid)==ratio*n, 'Validation set is right size'
    assert set(list(sd.train.items)+list(sd.valid.items))==set(range(n)), 'All items covered'
def test_fuzzy_line_match():
    this_tests(nbtest._fuzzy_line_match)
    # Testing _fuzzy_test_match private methods
    result = nbtest._fuzzy_line_match('Databunch.get', ['d = DataBunch()', 'item = d.get(5)'])
    assert len(result) == 1, 'finds class methods'

    result = nbtest._fuzzy_line_match('TextList', ['tl = (TextList.from_df()', '', 'LMTextList()'])
    assert len(result) == 1, 'matches classes'
示例#27
0
def test_custom_dataset():
    this_tests(DataBunch)
    tr_dataset = CustomDataset([1, 2, 3])
    val_dataset = CustomDataset([4, 5, 6])
    data = DataBunch.create(tr_dataset, val_dataset)

    # test property fallback
    assert data.loss_func == F.nll_loss
示例#28
0
def test_model2half_forward():
    this_tests(model2half)
    learn = fake_learner()
    x,y = next(iter(learn.data.train_dl))
    res1 = learn.model(x)
    learn.model = model2half(learn.model)
    res2 = learn.model(x.half())
    assert (res2.float() - res1).abs().sum() < 0.01
def test_all_dihedral():
    this_tests(dihedral)
    tfm = dihedral()
    img = img_test([0,1])
    targets = [[0,1], [4,1], [0,3], [4,3], [1,0], [1,4], [3,0], [3,4]]
    for k, t in enumerate(targets):
        tfm.resolved = {'k':k}
        check_image(img.apply_tfms(tfm, do_resolve=False), t)
def test_mask_data_aug():
    this_tests(Image, ImageSegment)
    points = torch.randint(0,2, ((1,64,64))).float()
    img, mask = Image(points), ImageSegment(points)
    lls = create_data(img, mask, 64, mode='nearest')
    tfm_x,tfm_y = lls.train[0]
    new_mask = (tfm_x.data[0] > 0.5)
    assert (new_mask.float() - tfm_y.data[0].float()).sum() < 1.
示例#31
0
def test_get_preds():
    learn = fake_learner()
    this_tests(learn.get_preds)
    with CaptureStdout() as cs:
        a = learn.get_preds()
    assert learn.data.batch_size == len(a[1])
def test_in_channels():
    this_tests(in_channels)
    m = simple_cnn(b)
    assert in_channels(m) == 3
示例#33
0
def test_this_tests():
    # function by reference (and self test)
    this_tests(this_tests)

    # multiple entries: same function twice on purpose, should result in just one entry,
    # but also testing multiple entries - and this test tests only a single function.
    this_tests(this_tests, this_tests)

    import fastai
    # explicit fully qualified function (requires all the sub-modules to be loaded)
    this_tests(fastai.gen_doc.doctest.this_tests)

    # explicit fully qualified function as a string
    this_tests('fastai.gen_doc.doctest.this_tests')

    # special case for situations where a test doesn't test fastai API or non-callable attribute
    this_tests('na')

    # not a real function
    func = 'foo bar'
    try:
        this_tests(func)
    except Exception as e:
        assert f"'{func}' is not a function" in str(e)
    else:
        assert False, f'this_tests({func}) should have failed'

    # not a function as a string that looks like fastai function, but it is not
    func = 'fastai.gen_doc.doctest.doesntexistreally'
    try:
        this_tests(func)
    except Exception as e:
        assert f"'{func}' is not a function" in str(e)
    else:
        assert False, f'this_tests({func}) should have failed'

    # not a fastai function
    import numpy as np
    func = np.any
    try:
        this_tests(func)
    except Exception as e:
        assert f"'{func}' is not in the fastai API" in str(e)
    else:
        assert False, f'this_tests({func}) should have failed'
示例#34
0
def test_class_anchor():
    this_tests('na')
    docstr   = "`DataBunch.create`, `DeviceDataLoader.proc_batch`"
    expected = "[`DataBunch.create`](/basic_data.html#DataBunch.create), [`DeviceDataLoader.proc_batch`](/basic_data.html#DeviceDataLoader.proc_batch)"
    imports = 'from fastai.basic_train import *'
    assert_link(docstr, expected, nb_cells=[gen_notebooks.get_code_cell(imports)])
def test_in_channels_groups():
    this_tests(in_channels)
    m = nn.Conv2d(10, 2, 3, groups=2)
    assert in_channels(m) == 10
示例#36
0
def test_link_typedef_double_bt():
    this_tests('na')
    docstr   = "- `ParamList` = `Collection`\[`nn`.`Parameter`]"
    expected = "- `ParamList` = `Collection`\[[`nn`](https://pytorch.org/docs/stable/nn.html#torch-nn).`Parameter`]"
    assert_link(docstr, expected)
示例#37
0
def test_link_inner_class_functions():
    this_tests('na')
    docstr   = "To train your model in mixed precision you just have to call `Learner.to_fp16`, which converts the model and modifies the existing `Learner` to add `MixedPrecision`."
    expected = "To train your model in mixed precision you just have to call [`Learner.to_fp16`](/train.html#to_fp16), which converts the model and modifies the existing [`Learner`](/basic_train.html#Learner) to add [`MixedPrecision`](/callbacks.fp16.html#MixedPrecision)."
    imports = 'from fastai.callbacks.fp16 import *'
    assert_link(docstr, expected, nb_cells=[gen_notebooks.get_code_cell(imports)])
示例#38
0
def test_link_typedef():
    this_tests('na')
    docstr   = "- `LayerFunc` = `Callable`\[`nn.Module`],`None`]"
    expected = "- `LayerFunc` = `Callable`\[[`nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)],`None`]"
    assert_link(docstr, expected, modules=[torch], msg='Type definitions to torch formatted incorrectly. See fastai_typing.ipynb')
示例#39
0
def test_gan_datasets(path):
    this_tests(GANItemList.from_folder)
    lls = GANItemList.from_folder(path).split_none().label_from_func(noop)

    assert len(lls.train) == 1428
    assert isinstance(lls.train.x, GANItemList)
示例#40
0
def test_torchvision():
    this_tests('na')
    docstr   = 'Note that `tvm` is the namespace we use for `torchvision.models`.'
    expected = 'Note that [`tvm`](https://pytorch.org/docs/stable/torchvision/models.html#torchvision.models) is the namespace we use for `torchvision.models`.'
    assert_link(docstr, expected, msg='Should match imported aliases')
def test_split_model():
    this_tests(split_model)
    m = simple_cnn(b)
    pool = split_model(m, [m[2][0]])[1][0]
    assert pool == m[2][0], "Did not properly split at adaptive pooling layer"
示例#42
0
def test_link_class_methods():
    this_tests('na')
    docstr   = "`ImageDataBunch.from_csv`"
    expected = "[`ImageDataBunch.from_csv`](/vision.data.html#ImageDataBunch.from_csv)"
    imports = 'from fastai.vision.data import *'
    assert_link(docstr, expected, nb_cells=[gen_notebooks.get_code_cell(imports)])
def test_range_children():
    this_tests(range_children)
    m = simple_cnn(b)
    assert len(range_children(m)) == 3
def test_keep_parameter():
    sa = SelfAttention(128)
    this_tests(SelfAttention)
    flat = nn.Sequential(*flatten_model(sa))
    for p in sa.parameters():
        assert id(p) in [id(a) for a in flat.parameters()]
def test_in_channels_no_weights():
    this_tests(in_channels)
    with pytest.raises(Exception) as e_info:
        in_channels(nn.Sequential())
    assert e_info.value.args[0] == 'No weight layer'
def test_batch_size_3():
    this_tests('na')
    _run_batch_size_test(3)
def test_requires_grad():
    this_tests(requires_grad)
    m = simple_cnn(b)
    assert requires_grad(m) == True
示例#48
0
def test_get_tests_dir():
    this_tests(nbtest.get_tests_dir)
    result: Path = nbtest.get_tests_dir(nbtest)
    assert result.parts[
        -1] == 'tests', f"Failed: get_tests_dir return unexpected result: {result}"
示例#49
0
def test_tensor_with_list():
    this_tests(tensor)
    r = tensor(a)
    assert torch.all(r == exp)
def test_has_version():
    this_tests('na')
    assert fastai.__version__
示例#51
0
def test_np2model_tensor():
    this_tests(np2model_tensor)
    a = np.ones([2, 2])
    t = np2model_tensor(a)
    assert isinstance(t, torch.FloatTensor)
示例#52
0
def test_trange_of():
    this_tests(trange_of)
    t = trange_of(a)
    assert len(t) == len(a)
def test_to_np():
    this_tests(to_np)
    a = to_np(exp)
    assert isinstance(a, np.ndarray)
示例#54
0
def test_check_perf(capsys):
    this_tests(check_perf)
    check_perf()
    captured = capsys.readouterr()
def test_tensor_with_tensor():
    this_tests(tensor)
    c = torch.tensor(a)
    r = tensor(c)
    assert r.data_ptr() == c.data_ptr()
    assert torch.all(r == exp)
def test_default_fill_strategy_is_median():
    fill_missing_transform = FillMissing([], [])

    assert fill_missing_transform.fill_strategy is FillStrategy.MEDIAN
    this_tests(FillMissing)
def test_last_layer():
    this_tests(last_layer)
    m = nn.Sequential(nn.Linear(2, 2), nn.ReLU())
    ll = last_layer(m)
    assert isinstance(ll, nn.Module)
    assert isinstance(ll, torch.nn.modules.activation.ReLU)
示例#58
0
def test_val_loss(learn):
    this_tests(learn.validate, CollabDataBunch.from_df, collab_learner)
    assert learn.validate()[0] < 0.8
示例#59
0
def test_fastai_prefix():
    this_tests('na')
    docstr   = "functions for your application (`fastai.vision`)"
    expected = "functions for your application ([`fastai.vision`](/vision.html#vision))"
    assert_link(docstr, expected, msg='Should match keywords prefixed with fastai. See `index.ipynb`')
示例#60
0
def test_setup_parser():
    this_tests("na")
    assert data["name"] == "fastai"

    # print(data['extras_require'])
    assert "dev" in data["extras_require"]