title: How to use Tensorboard with Pytorch in Sagemaker

body:
I am using torch version 1.2.0 and installed with conda tensorboard 2.0.2. I am running on Sagemaker conda_pytorch_p36. I also followed this StackOverflow question from a while ago.

My goal is to run:

from torch.utils.tensorboard import SummaryWriter

but it returns the following error:

TypeError: __new__() got an unexpected keyword argument 'serialized_options'

Any insights?

Installed packages in my conda env are the following:

# packages in environment at /home/ec2-user/anaconda3/envs/pytorch_p36:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main
alabaster                 0.7.10           py36h306e16b_0
anaconda-client           1.6.14                   py36_0
anaconda-project          0.8.2            py36h44fb852_0
argparse                  1.4.0                     <pip>
asn1crypto                0.24.0                   py36_0
astroid                   1.6.3                    py36_0
astropy                   3.0.2            py36h3010b51_1
attrs                     18.1.0                   py36_0
autovizwidget             0.13.1                    <pip>
awscli                    1.16.283                  <pip>
babel                     2.5.3                    py36_0
backcall                  0.1.0                    py36_0
backports                 1.0              py36hfa02d7e_1
backports.shutil_get_terminal_size 1.0.0            py36hfea85ff_2
bcrypt                    3.1.7                     <pip>
beautifulsoup4            4.6.0            py36h49b8c8c_1
bitarray                  0.8.1            py36h14c3975_1
bkcharts                  0.2              py36h735825a_0
blas                      1.0                         mkl
blaze                     0.11.3           py36h4e06776_0
bleach                    2.1.3                    py36_0
blosc                     1.14.3               hdbcaa40_0
bokeh                     1.0.4                    py36_0
boto                      2.48.0           py36h6e4cd66_1
boto3                     1.10.19                   <pip>
boto3                     1.9.234                    py_0
botocore                  1.12.234                   py_0
botocore                  1.13.19                   <pip>
bottleneck                1.2.1            py36haac1ea0_0
bzip2                     1.0.6                h14c3975_5
ca-certificates           2019.10.16                    0
cached-property           1.5.1                     <pip>
cairo                     1.14.12              h8948797_3
certifi                   2019.9.11                py36_0
cffi                      1.11.5           py36h9745a5d_0
chardet                   3.0.4            py36h0f667ec_1
click                     6.7              py36h5253387_0
cloudpickle               0.5.3                    py36_0
clyent                    1.2.2            py36h7e57e65_1
colorama                  0.3.9            py36h489cec4_0
contextlib2               0.5.5            py36h6c84a62_0
cryptography              2.8                       <pip>
cryptography              2.2.2            py36h14c3975_0
cudatoolkit               10.0.130                      0
curl                      7.60.0               h84994c4_0
cycler                    0.10.0           py36h93f1223_0
cymem                     2.0.2            py36he1b5a44_0    fastai
cython                    0.28.2           py36h14c3975_0
cython-blis               0.2.4            py36h516909a_1    fastai
cytoolz                   0.9.0.1          py36h14c3975_0
dask                      0.17.5                   py36_0
dask-core                 0.17.5                   py36_0
dataclasses               0.6                        py_0    fastai
datashape                 0.5.4            py36h3ad6b5c_0
dbus                      1.13.2               h714fa37_1
decorator                 4.3.0                    py36_0
defusedxml                0.6.0                      py_0
distributed               1.21.8                   py36_0
docker                    3.7.3                     <pip>
docker-compose            1.24.1                    <pip>
docker-pycreds            0.4.0                     <pip>
dockerpty                 0.4.1                     <pip>
docopt                    0.6.2                     <pip>
docutils                  0.14             py36hb0f60f5_0
entrypoints               0.2.3            py36h1aec115_2
environment-kernels       1.1.1                     <pip>
et_xmlfile                1.0.1            py36hd6bccc3_0
expat                     2.2.5                he0dffb1_0
fabric                    2.5.0                     <pip>
fastai                    1.0.59                        1    fastai
fastcache                 1.0.2            py36h14c3975_2
fastprogress              0.1.21                     py_0    fastai
filelock                  3.0.4                    py36_0
flask                     1.0.2                    py36_1
flask-cors                3.0.4                    py36_0
fontconfig                2.13.0               h9420a91_0
freetype                  2.9.1                h8a8886c_1
fribidi                   1.0.5                h7b6447c_0
get_terminal_size         1.0.0                haa9412d_0
gevent                    1.3.0            py36h14c3975_0
glib                      2.56.1               h000015b_0
glob2                     0.6              py36he249c77_0
gmp                       6.1.2                h6c8ec71_1
gmpy2                     2.0.8            py36hc8893dd_2
graphite2                 1.3.11               h16798f4_2
graphviz                  2.40.1               h21bd128_2
greenlet                  0.4.13           py36h14c3975_0
gst-plugins-base          1.14.0               hbbd80ab_1
gstreamer                 1.14.0               hb453b48_1
h5py                      2.8.0            py36h989c5e5_3
harfbuzz                  1.8.8                hffaf4a1_0
hdf5                      1.10.2               hba1933b_1
hdijupyterutils           0.13.1                    <pip>
heapdict                  1.0.0                    py36_2
html5lib                  1.0.1            py36h2f9c1c0_0
icu                       58.2                 h9c2bf20_1
idna                      2.6              py36h82fb2a8_1
imageio                   2.3.0                    py36_0
imagesize                 1.0.0                    py36_0
intel-openmp              2018.0.0                      8
invoke                    1.3.0                     <pip>
ipykernel                 4.8.2                    py36_0
ipyparallel               6.2.2                     <pip>
ipython                   6.4.0                    py36_0
ipython_genutils          0.2.0            py36hb52b0d5_0
ipywidgets                7.2.1                    py36_0
ipywidgets                7.4.0                     <pip>
isort                     4.3.4                    py36_0
itsdangerous              0.24             py36h93cc618_1
jbig                      2.1                  hdba287a_0
jdcal                     1.4                      py36_0
jedi                      0.12.0                   py36_1
jinja2                    2.10             py36ha16c418_0
jmespath                  0.9.4                      py_0
jpeg                      9b                   h024ee3a_2
jsonschema                2.6.0            py36h006f8b5_0
jupyter                   1.0.0                    py36_4
jupyter_client            5.2.3                    py36_0
jupyter_console           5.2.0            py36he59e554_1
jupyter_core              4.4.0            py36h7c827e3_0
jupyterlab                0.32.1                   py36_0
jupyterlab_launcher       0.10.5                   py36_0
kiwisolver                1.0.1            py36h764f252_0
krb5                      1.14.2               hcdc1b81_6
lazy-object-proxy         1.3.1            py36h10fcdad_0
libcurl                   7.60.0               h1ad7b7a_0
libedit                   3.1.20170329         h6b74fdf_2
libffi                    3.2.1                hd88cf55_4
libgcc-ng                 9.1.0                hdf63c60_0
libgfortran               3.0.0                         1    conda-forge
libgfortran-ng            7.2.0                hdf63c60_3
libpng                    1.6.37               hbc83047_0
libprotobuf               3.5.2                hd28b015_1    conda-forge
libsodium                 1.0.16               h1bed415_0
libssh2                   1.8.0                h9cfc8f7_4
libstdcxx-ng              9.1.0                hdf63c60_0
libtiff                   4.0.9                he85c1e1_1
libtool                   2.4.6                h544aabb_3
libuuid                   1.0.3                h1bed415_2
libxcb                    1.13                 h1bed415_1
libxml2                   2.9.8                h26e45fe_1
libxslt                   1.1.32               h1312cb7_0
llvmlite                  0.23.1           py36hdbcaa40_0
locket                    0.2.0            py36h787c0ad_1
lxml                      4.2.1            py36h23eabaa_0
lzo                       2.10                 h49e0be7_2
markupsafe                1.0              py36hd9260cd_1
matplotlib                2.2.2                     <pip>
matplotlib                3.0.3            py36h5429711_0
mccabe                    0.6.1            py36h5ad9710_1
mistune                   0.8.3            py36h14c3975_1
mkl                       2018.0.3                      1
mkl-service               1.1.2            py36h17a0993_4
mkl_fft                   1.0.6            py36h7dd41cf_0
mkl_random                1.0.1            py36h629b387_0
mock                      3.0.5                     <pip>
more-itertools            4.1.0                    py36_0
mpc                       1.0.3                hec55b23_5
mpfr                      3.1.5                h11a74b3_2
mpi                       1.0                     openmpi    conda-forge
mpmath                    1.0.0            py36hfeacd6b_2
msgpack                   0.6.0                     <pip>
msgpack-python            0.5.6            py36h6bb024c_0
multipledispatch          0.5.0                    py36_0
murmurhash                1.0.2            py36he6710b0_0
nb_conda                  2.2.1                    py36_2    conda-forge
nb_conda_kernels          2.2.2                    py36_0    conda-forge
nbconvert                 5.4.1                    py36_3
nbformat                  4.4.0            py36h31c9010_0
ncurses                   6.1                  hf484d3e_0
networkx                  2.1                      py36_0
ninja                     1.9.0            py36hfd86e86_0
nltk                      3.3.0                    py36_0
nose                      1.3.7            py36hcdf7029_2
notebook                  5.5.0                    py36_0
numba                     0.38.0           py36h637b7d7_0
numexpr                   2.6.5            py36h7bf3b9c_0
numpy                     1.15.4           py36h1d66e8a_0
numpy-base                1.15.4           py36h81de0dd_0
numpydoc                  0.8.0                    py36_0
nvidia-ml-py3             7.352.0                    py_0    fastai
odo                       0.5.1            py36h90ed295_0
olefile                   0.45.1                   py36_0
onnx                      1.5.0                     <pip>
openmpi                   3.1.0                h26a2512_3    conda-forge
openpyxl                  2.5.3                    py36_0
openssl                   1.0.2t               h7b6447c_1
packaging                 17.1                     py36_0
pandas                    0.24.2           py36he6710b0_0
pandoc                    1.19.2.1             hea2e7c5_1
pandocfilters             1.4.2            py36ha6701b7_1
pango                     1.42.3               h8589676_0
paramiko                  2.6.0                     <pip>
parso                     0.2.0                    py36_0
partd                     0.3.8            py36h36fd896_0
patchelf                  0.9                  hf79760b_2
path.py                   11.0.1                   py36_0
pathlib2                  2.3.2                    py36_0
patsy                     0.5.0                    py36_0
pcre                      8.42                 h439df22_0
pep8                      1.7.1                    py36_0
pexpect                   4.5.0                    py36_0
pickleshare               0.7.4            py36h63277f8_0
pillow                    5.2.0            py36heded4f4_0
pip                       10.0.1                   py36_0
pixman                    0.34.0               hceecf20_3
pkginfo                   1.4.2                    py36_1
plac                      0.9.6                    py36_0
plotly                    4.2.1                     <pip>
pluggy                    0.6.0            py36hb689045_0
ply                       3.11                     py36_0
preshed                   2.0.1            py36he6710b0_0
prompt_toolkit            1.0.15           py36h17d85b1_0
protobuf                  3.5.2            py36hd28b015_0    conda-forge
protobuf3-to-dict         0.1.5                     <pip>
psutil                    5.4.5            py36h14c3975_0
psycopg2                  2.7.5                     <pip>
ptyprocess                0.5.2            py36h69acd42_0
py                        1.5.3                    py36_0
py4j                      0.10.7                    <pip>
pyasn1                    0.4.8                     <pip>
pycodestyle               2.4.0                    py36_0
pycosat                   0.6.3            py36h0a5515d_0
pycparser                 2.18             py36hf9f622e_1
pycrypto                  2.6.1            py36h14c3975_8
pycurl                    7.43.0.1         py36hb7f436b_0
pyflakes                  1.6.0            py36h7bd6a15_0
pygal                     2.4.0                     <pip>
pygments                  2.2.0            py36h0d3125c_0
pykerberos                1.2.1            py36h14c3975_0
pylint                    1.8.4                    py36_0
PyNaCl                    1.3.0                     <pip>
pyodbc                    4.0.23           py36hf484d3e_0
pyopenssl                 18.0.0                   py36_0
pyparsing                 2.2.0            py36hee85983_1
pyqt                      5.9.2            py36h751905a_0
pysocks                   1.6.8                    py36_0
pyspark                   2.3.2                     <pip>
pytables                  3.4.3            py36h02b9ad4_2
pytest                    3.5.1                    py36_0
pytest-arraydiff          0.2                      py36_0
pytest-astropy            0.3.0                    py36_0
pytest-doctestplus        0.1.3                    py36_0
pytest-openfiles          0.3.0                    py36_0
pytest-remotedata         0.2.1                    py36_0
python                    3.6.5                hc3d631a_2
python-dateutil           2.7.3                    py36_0
pytorch                   1.2.0           py3.6_cuda10.0.130_cudnn7.6.2_0    pytorch
pytz                      2018.4                   py36_0
pywavelets                0.5.2            py36he602eb0_0
pyyaml                    3.12             py36hafb9ca4_1
pyzmq                     17.0.0           py36h14c3975_0
qt                        5.9.6                h52aff34_0
qtawesome                 0.4.4            py36h609ed8c_0
qtconsole                 4.3.1            py36h8f73b5b_0
qtpy                      1.4.1                    py36_0
readline                  7.0                  ha6073c6_4
requests                  2.20.0                py36_1000    conda-forge
requests-kerberos         0.12.0                    <pip>
retrying                  1.3.3                     <pip>
rope                      0.10.7           py36h147e2ec_0
rsa                       3.4.2                     <pip>
ruamel_yaml               0.15.35          py36h14c3975_1
s3fs                      0.1.5                    py36_0
s3transfer                0.2.1                    py36_0
sagemaker                 1.43.4.post1              <pip>
sagemaker-pyspark         1.2.6                     <pip>
scikit-image              0.13.1           py36h14c3975_1
scikit-learn              0.20.3                    <pip>
scikit-learn              0.19.1           py36h7aa7ec6_0
scipy                     1.1.0            py36hfc37229_0
seaborn                   0.8.1            py36hfad7ec4_0
send2trash                1.5.0                    py36_0
setuptools                39.1.0                   py36_0
simplegeneric             0.8.1                    py36_2
singledispatch            3.4.0.3          py36h7a266c3_0
sip                       4.19.8           py36hf484d3e_0
six                       1.11.0           py36h372c433_1
snappy                    1.1.7                hbae5bb6_3
snowballstemmer           1.2.1            py36h6febd40_0
sortedcollections         0.6.1                    py36_0
sortedcontainers          1.5.10                   py36_0
spacy                     2.1.8            py36hc9558a2_0    fastai
sparkmagic                0.12.5                    <pip>
sphinx                    1.7.4                    py36_0
sphinxcontrib             1.0              py36h6d0f590_1
sphinxcontrib-websupport  1.0.1            py36hb5cb234_1
spyder                    3.2.8                    py36_0
sqlalchemy                1.2.7            py36h6b74fdf_0
SQLAlchemy                1.2.11                    <pip>
sqlite                    3.23.1               he433501_0
srsly                     0.1.0            py36he1b5a44_0    fastai
statsmodels               0.9.0            py36h3010b51_0
sympy                     1.1.1            py36hc6d1c1c_0
tblib                     1.3.2            py36h34cf8b6_0
terminado                 0.8.1                    py36_1
testpath                  0.3.1            py36h8cadb63_0
texttable                 0.9.1                     <pip>
thinc                     7.0.8            py36hc9558a2_0    fastai
tk                        8.6.8                hbc83047_0
toolz                     0.9.0                    py36_0
torchvision               0.4.0                py36_cu100    pytorch
tornado                   5.0.2                    py36_0
tqdm                      4.36.1                     py_0
traitlets                 4.3.2            py36h674d592_0
typing                    3.6.4                    py36_0
typing-extensions         3.7.4.1                   <pip>
unicodecsv                0.14.1           py36ha668878_0
unixodbc                  2.3.6                h1bed415_0
urllib3                   1.23                     py36_0
wasabi                    0.2.2                      py_0    fastai
wcwidth                   0.1.7            py36hdf4376a_0
webencodings              0.5.1            py36h800622e_1
websocket-client          0.56.0                    <pip>
werkzeug                  0.14.1                   py36_0
wheel                     0.31.1                   py36_0
widgetsnbextension        3.4.2                     <pip>
widgetsnbextension        3.2.1                    py36_0
wrapt                     1.10.11          py36h28b7045_0
xlrd                      1.1.0            py36h1db9f0c_1
xlsxwriter                1.0.4                    py36_0
xlwt                      1.3.0            py36h7b00a1f_0
xz                        5.2.4                h14c3975_4
yaml                      0.1.7                had09818_2
zeromq                    4.2.5                h439df22_0
zict                      0.1.3            py36h3a3bf81_0
zlib                      1.2.11               ha838bed_2

Simple mnist example:

import sagemaker #needed later to spin a training job
import torch
import torchvision
import torchvision.transforms as transforms
import tensorboard
from torch.utils.tensorboard import SummaryWriter

train_set = torchvision.datasets.MNIST(
    root='./data',
    train=True,
    download=True,
    transform=transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]))

train_loader = torch.utils.data.DataLoader(train_set, batch_size=32)

images, labels = next(iter(train_loader))

tb = SummaryWriter()
images, labels = next(iter(train_loader))
grid = torchvision.utils.make_grid(images)
tb.add_image('images', grid)
tb.close()


title: tensorflow how to pad batched text like pytorch's 'collate_fn'?

body:
I want to pad a batch of text into same length, generate segment id, mask vector, and then feed them to bert model.
In pytorch, I can use the collate_fn like below.

def collate_fn(self, batch):
    rows = self.df.iloc[batch] # take a batch of data
    ids, seg_ids = self.get_ids_segs(rows) # process data
    attention_mask = (ids > 0)
    return ids, seg_ids,attention_mask

But in tensorflow, the data is pass by a tuple of matrix, thus all the text are padded into the max length 512.

# ids.shape = seg_ids = attention_mask = (data_number, max_seq_len)
xs = (ids, seg_ids, attention_mask)

model.fit(xs,, ys, batch_size=batch_size)

I found tf.data.dataset has a function padded_batch. But it can only pad one input, what I have is 3 input data, ids, seq_ids, attn_mask.


title: What value should I set for `nn.Linear(1024, 256)` when the dimensions of the dataset are [64 x 25088]?

body:
I keep getting an error in pytorch that the size I have entered is wrong:

  RuntimeError: size mismatch, m1: [64 x 25088], m2: [1024 x 256] at /opt/conda/conda-bld/pytorch_1524584710464/work/aten/src/THC/generic/THCTensorMathBlas.cu:249

If I set nn.Linear to 64 x 25088, I get a similar error:

  RuntimeError: size mismatch, m1: [64 x 25088], m2: [64 x 25088] at /opt/conda/conda-bld/pytorch_1524584710464/work/aten/src/THC/generic/THCTensorMathBlas.cu:249

What is going on?

This is the code that is generating the error:

model = models.vgg16(pretrained=True)

# Freeze parameters so we don't backprop through them
for param in model.parameters():
    param.requires_grad = False

model.classifier = nn.Sequential(nn.Linear(64, 25088),
                                 nn.ReLU(),
                                 nn.Dropout(0.2),
                                 nn.Linear(256, 2),
                                 nn.LogSoftmax(dim=1))

criterion = nn.NLLLoss()

# Only train the classifier parameters, feature parameters are frozen
optimizer = optim.Adam(model.classifier.parameters(), lr=0.003)

# Use GPU if it's available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device);

epochs = 1
steps = 0
running_loss = 0
print_every = 5

with active_session():
    for epoch in range(epochs):
        for inputs, labels in trainloader:
            steps += 1
            # Move input and label tensors to the default device
            inputs, labels = inputs.to(device), labels.to(device)

            optimizer.zero_grad()

            logps = model.forward(inputs)
            loss = criterion(logps, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()

            if steps % print_every == 0:
                test_loss = 0
                accuracy = 0
                model.eval()
                with torch.no_grad():
                    for inputs, labels in testloader:
                        inputs, labels = inputs.to(device), labels.to(device)
                        logps = model.forward(inputs)
                        batch_loss = criterion(logps, labels)

                        test_loss += batch_loss.item()

                        # Calculate accuracy
                        ps = torch.exp(logps)
                        top_p, top_class = ps.topk(1, dim=1)
                        equals = top_class == labels.view(*top_class.shape)
                        accuracy += torch.mean(equals.type(torch.FloatTensor)).item()

                print(f"Epoch {epoch+1}/{epochs}.. "
                      f"Train loss: {running_loss/print_every:.3f}.. "
                      f"Test loss: {test_loss/len(testloader):.3f}.. "
                      f"Test accuracy: {accuracy/len(testloader):.3f}")
                running_loss = 0
                model.train()


title: How to use a customized dataset for training with PyTorch/few-shot-vid2vid

body:
I’d like to use my own dataset created from the FaceForensics footage with few-show-vid2vid. So I generated image sequences with ffmpeg and keypoints with dlib. When I try to start the training script, I get the following error. What exactly is the problem? The provided small dataset was working for me.

CustomDatasetDataLoader
485 sequences
dataset [FaceDataset] was created
Resuming from epoch 1 at iteration 0
create web directory ./checkpoints/face/web...
---------- Networks initialized -------------
---------- Optimizers initialized -------------
./checkpoints/face/latest_net_G.pth not exists yet!
./checkpoints/face/latest_net_D.pth not exists yet!
model [Vid2VidModel] was created
Traceback (most recent call last):
  File "train.py", line 73, in <module>
    train()
  File "train.py", line 40, in train
    for idx, data in enumerate(dataset, start=trainer.epoch_iter):
  File "/home/keno/.local/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 819, in __next__
    return self._process_data(data)
  File "/home/keno/.local/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 846, in _process_data
    data.reraise()
  File "/home/keno/.local/lib/python3.7/site-packages/torch/_utils.py", line 369, in reraise
    raise self.exc_type(msg)
IndexError: Caught IndexError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/keno/.local/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/keno/.local/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/keno/.local/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/keno/repos/few-shot-vid2vid/data/fewshot_face_dataset.py", line 103, in __getitem__
    Li = self.get_face_image(keypoints, transform_L, ref_img.size)
  File "/home/keno/repos/few-shot-vid2vid/data/fewshot_face_dataset.py", line 168, in get_face_image
    x = keypoints[sub_edge, 0]
IndexError: index 82 is out of bounds for axis 0 with size 82

EDIT: How I set up my dataset. I created image sequences from the video footage with ffmpeg -i _video_ -o %05d.jpg, following the directory structure of the provided sample dataset. Then I generated the keypoints by using landmark detection with dlib, based on the code example provided on the dlib website. I expanded the sample code to 68 points and saved them to .txt files:

import re
import sys
import os
import dlib
import glob

# if len(sys.argv) != 4:
#     print(
#         "Give the path to the trained shape predictor model as the first "
#         "argument and then the directory containing the facial images.\n"
#         "For example, if you are in the python_examples folder then "
#         "execute this program by running:\n"
#         "    ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces\n"
#         "You can download a trained facial shape predictor from:\n"
#         "    http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2")
#     exit()

predictor_path = sys.argv[1]
faces_folder_path = sys.argv[2]
text_file_path = sys.argv[3]

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()

for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
    file_number = os.path.split(f)
    print(file_number[1])
    file_number = os.path.splitext(file_number[1])
    file_number = file_number[0]
    export_path = os.path.join(text_file_path, '%s.txt' % file_number)
    text = open(export_path,"w+")

    print("Processing file: {}".format(f))
    img = dlib.load_rgb_image(f)

    win.clear_overlay()
    win.set_image(img)

    # Ask the detector to find the bounding boxes of each face. The 1 in the
    # second argument indicates that we should upsample the image 1 time. This
    # will make everything bigger and allow us to detect more faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        # Get the landmarks/parts for the face in box d.
        shape = predictor(img, d)
        for i in range(67):
            result = str(shape.part(i))
            result = result.strip("()")
            print(result)
            text.write(result + '\n')
        # Draw the face landmarks on the screen.
        win.add_overlay(shape)

    text.close()
    win.add_overlay(dets)


title: Pytorch tensor allocation much slower inside docker container

body:
Running the snippet below takes twice as long when run inside a docker-container (keeping all python versions the same) using the Nvidia-docker runtime, then locally executed.
I tried various different base images, but they have a similar (slower) runtime. I would have expected that running code inside a Docker container does not impact runtime performance? So I feel that I'm missing something.
import numpy as np
from time import time
import torch
def run():
    print("doing run")
    random_data = []
    for _ in range(320):
        random_data.append(np.random.randint(256, size=(1, 84, 84)))
    tensor = torch.tensor(random_data, device="cuda")
    print(tensor.shape)
n_runs = int(1e3)
runtimes = []
for i in range(n_runs):
    start = time()
    run()
    end = time()
    took = end-start
    runtimes.append(took)
    print(f"It took {took} second")
print("====")
print(f"avg_runtime: {np.average(runtimes)}")


title: Object detection torchvision : IOError: [Errno 2] No such file or directory:

body:
I'm working on clothes detection in an image. For this, i'm using the package TorchVision on python, and i want to start from a pretrained model and to fintetune it for my classes (different kind of clothes). My images are in a zip file. When i run the function train_one_epoch, i obtain this error :

IOError: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/torch/utils/data/_utils/worker.py", line 99, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "<ipython-input-16-4557e2f6ca8b>", line 51, in __getitem__
    img = Image.open(img_path).convert("RGB")
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2530, in open
    fp = builtins.open(filename, "rb")
IOError: [Errno 20] Not a directory: 'gdrive/My Drive/donnees_kaggle_iMaterialist/train.zip/062fc2042f49d10fa625151cb652c3da.jpg'

Here is a part of my code :

python

num_classes = 46 + 1
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

dataset_train = FashionDataset('gdrive/My Drive/donnees_kaggle_iMaterialist/train.zip/', # "../input/imaterialist-fashion-2019-FGVC6/train/",
                               'gdrive/My Drive/donnees_kaggle_iMaterialist/train.csv.zip', #"../input/imaterialist-fashion-2019-FGVC6/train.csv",
                               256,
                               256,
                               transforms=get_transform(train=True))

model_ft = get_instance_segmentation_model(num_classes)
model_ft.to(device)

data_loader = torch.utils.data.DataLoader(
    dataset_train, batch_size=4, shuffle=True, num_workers=8,
    collate_fn=lambda x: tuple(zip(*x)))

params = [p for p in model_ft.parameters() if p.requires_grad]
optimizer = torch.optim.SGD(params, lr=0.001,
                            momentum=0.9, weight_decay=0.0005)

lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                               step_size=5,
                                               gamma=0.1)
num_epochs = 10

for epoch in range(num_epochs):
    train_one_epoch(model_ft, optimizer, data_loader, device, epoch, print_freq=10)
    lr_scheduler.step()

torch.save(model_ft.state_dict(), "model.bin")


title: Whenever I try to install torch, it displays killed

body:
I just want to install pytorch, I ran this in the terminal:

pip install torch

And it displays:

Collecting torch
Killed

What is the problem?


title: ImportError: cannot import name 'hf_bucket_url' in HuggingFace Transformers

body:
So I installed the latest version of transformers on Google Colab
!pip install transformers

When trying to invoke the conversion file using
!python /usr/local/lib/python3.6/dist-packages/transformers/convert_pytorch_checkpoint_to_tf2.py .py --help

Or trying to use
from transformers.file_utils import hf_bucket_url.                                 // works
from transformers.convert_pytorch_checkpoint_to_tf2 import *.                      // fails

convert_pytorch_checkpoint_to_tf("gpt2", pytorch_file, config_file, tf_file).

I get this error
 ImportError                               Traceback (most recent call last)

<ipython-input-3-dadaf83ecea0> in <module>()
      1 from transformers.file_utils import hf_bucket_url
----> 2 from transformers.convert_pytorch_checkpoint_to_tf2 import *
      3
      4 convert_pytorch_checkpoint_to_tf("gpt2", pytorch_file, config_file, tf_file)
/usr/local/lib/python3.6/dist-packages/transformers/convert_pytorch_checkpoint_to_tf2.py in <module>()
     20 import os
     21
---> 22 from transformers import (
     23     ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
     24     BERT_PRETRAINED_CONFIG_ARCHIVE_MAP,

ImportError: cannot import name 'hf_bucket_url'

What's going on?


title: Efficient predictions from pytorch model (transformers library) for NLP tasks with a GPU

body:
I have a huge data set of tweet texts (almost 3 billion tweets). I finetuned a BERT model with an annotated data set for a classification task.
Is there a way to make predictions more efficient and faster? I have access to one GPU only.
At the moment I use the following code:
import pandas as pd

import sys

from transformers import pipeline

judge = pipeline(
task="sentiment-analysis",
model="/trained_disasterlabels",
tokenizer='bert-base-uncased',
device=0)

id_disaster=sys.argv[1]

path="/disaster_data/ids_"+id_disaster+"_text"

open(path+"_labelpred", 'w').close()

n=200
with open(path,"rb") as f:
    batch=[]
    for line in f:
        batch.append(line.decode().rstrip("\n"))
        if len(batch)==n:

            preds=judge(batch)
            preds_labels=[x['label'].replace("LABEL_","") for x in preds]
            preds_probs=[round(x['score'],4) for x in preds]

            valsdf=pd.DataFrame({"labels":preds_labels,"probs":preds_probs})

            valsdf.to_csv(path+"_labelpred", mode='a', header=False,index=False)

            batch=[]

    preds=judge(batch)
    preds_labels=[x['label'].replace("LABEL_","") for x in preds]
    preds_probs=[round(x['score'],4) for x in preds]

    valsdf=pd.DataFrame({"labels":preds_labels,"probs":preds_probs})

    valsdf.to_csv(path+"_labelpred", mode='a', header=False,index=False)

Before I also experimented with the following:
model=BertForSequenceClassification.from_pretrained("/trained_disasterlabels")

model.eval()

model.to('cuda')

tokenizer=BertTokenizer.from_pretrained('bert-base-uncased')

import torch.nn.functional as F

path="/disaster_data/ids_"+id_disaster+"_text"

open(path+"_labelpred", 'w').close()

n=100

with open(path,"rb") as f:
    batch=[]
    for line in f:
        batch.append(line.decode().rstrip("\n"))
        if len(batch)==n:
           input_ids = tokenizer.batch_encode_plus(batch, add_special_tokens=True,return_tensors = 'pt', padding=True, truncation=True)

            input_ids.to('cuda')

            with torch.no_grad():
                last_hidden_states = model(**input_ids)

            temp_cpu = F.softmax(last_hidden_states[0],dim=1).detach().cpu().numpy()

            valsdf=pd.DataFrame(temp_cpu.tolist(),columns=["prob","labelpred"])

            batch=[]

    input_ids = tokenizer.batch_encode_plus(batch, add_special_tokens=True,return_tensors = 'pt', padding=True, truncation=True)

    with torch.no_grad():
        last_hidden_states = model(**input_ids)

    temp_cpu = F.softmax(last_hidden_states[0],dim=1).detach().cpu().numpy()

    valsdf=pd.DataFrame(temp_cpu.tolist(),columns=["labelpred","prob"])

    valsdf.to_csv(path+"_labelpred", mode='a', header=False,index=False)

    batch=[]


title: how to avoid split and sum of pieces in pytorch or numpy

body:
I want to split a long vector into smaller unequal pieces, do a summation on each piece and gather the results into a new vector.
I need to do this in pytorch but I am also interested to see how this is done with numpy.
This can easily be accomplish by splitting the vector.
sizes = [3, 7, 5, 9]
X = torch.ones(sum(sizes))
Y = torch.tensor([s.sum() for s in torch.split(X, sizes)])

or with np.ones and np.split.
Is there a more efficient way to do this?
Edit:
Inspired by the first comment:
indices = np.cumsum([0]+sizes)[:-1]
Y = np.add.reduceat(X, indices.tolist())

solves it for numpy. I am still looking for a solution with pytorch.


title: nn.NLLLoss() target size issue

body:
I have a label of size [32,2,10] and I want to separate this label into label1 and label2, with which I need to compute loss against the input for each label1 and label2.

Model is VGG16 + additional classifier.

output classes = 10

class cust_vgg():
    def forward(self, images,model):
        out = model.features(images)
        out1 = model.avgpool(out)
        out1 = out1.reshape(out1.size(0), -1)
        return model.classifier(out1), model.classifier1(out1)

out = cust_vgg.forward(images,model)
out_classifier,out_classifier1 = out
labels1,labels2 = labels[0]
loss_classifier = nn.NLLLoss(out_classifier, labels1)
loss_classifier1 = nn.NLLLoss(out_classifier1,labels2)
loss = loss_classifier + loss_classifier1
loss.backward()
opt.step()

  Input batch_size should match with the target batch_size for
  nn.NLLLoss()

  Error: Expected input batch_size (32) to match target batch_size (10).


title: Binary cross entropy Vs categorical cross entropy with 2 classes

body:
When considering the problem of classifying an input to one of 2 classes, 99% of the examples I saw used a NN with a single output and sigmoid as their activation followed by a binary cross-entropy loss.  Another option that I thought of is having the last layer produce 2 outputs and use a categorical cross-entropy with C=2 classes, but I never saw it in any example.
Is there any reason for that?

Thanks


title: python3: symbol lookup error: python3: undefined symbol: _Py_LegacyLocaleDetected when using slurm

body:
Good morning, i´ve encountered the following error message python3: symbol lookup error: python3: undefined symbol: _Py_LegacyLocaleDetected when trying to run a script using slurm.
the script is a deep learning task using pytorch, torchvision, tensorboard and configparser.
tensorboard and configparser are installed in the venv that is used, pytorch is loaded as module from the server module load PyTorch/1.1.0-foss-2019a-Python-3.6.8
I think the error has to do something with versioning, but i haven´t found anything useful on google.
any ideas how to fix it?
Here´s a list of all loaded modules:
Currently Loaded Modules:

binutils/.2.31.1-GCCcore-8.2.0              (H)
zlib/.1.2.11-GCCcore-8.2.0                  (H)
numactl/.2.0.12-GCCcore-8.2.0               (H)
XZ/.5.2.4-GCCcore-8.2.0                     (H)
libxml2/.2.9.8-GCCcore-8.2.0                (H)
libpciaccess/.0.14-GCCcore-8.2.0            (H)
hwloc/.1.11.11-GCCcore-8.2.0                (H)
OpenMPI/.3.1.3-GCC-8.2.0-2.31.1             (H)
OpenBLAS/.0.3.5-GCC-8.2.0-2.31.1            (H)
GCC/8.2.0-2.31.1
gompi/.2019a                                (H)
FFTW/.3.3.8-gompi-2019a                     (H)
ScaLAPACK/.2.0.2-gompi-2019a-OpenBLAS-0.3.5 (H)
foss/2019a
StdEnv
GCCcore/.8.2.0                              (H)
bzip2/.1.0.6-GCCcore-8.2.0                  (H)
ncurses/.6.1-GCCcore-8.2.0                  (H)
libreadline/.7.0-GCCcore-8.2.0              (H)
Tcl/8.6.9-GCCcore-8.2.0
SQLite/.3.27.2-GCCcore-8.2.0                (H)
GMP/.6.1.2-GCCcore-8.2.0                    (H)
libffi/.3.2.1-GCCcore-8.2.0                 (H)
expat/.2.2.6-GCCcore-8.2.0                  (H)
libpng/.1.6.36-GCCcore-8.2.0                (H)
freetype/.2.10.0-GCCcore-8.2.0              (H)
util-linux/.2.33.1-GCCcore-8.2.0            (H)
fontconfig/.2.13.1-GCCcore-8.2.0            (H)
X11/.20180604-GCCcore-8.2.0                 (H)
Tk/8.6.9-GCCcore-8.2.0
Python/3.6.8-foss-2019a
CUDA/10.1.105
cuDNN/7.5.0-CUDA-10.1.105
magma/.2.4.0-foss-2019a-CUDA-10.1.105       (H)
libyaml/.0.2.2-GCCcore-8.2.0                (H)
PyYAML/.5.1-foss-2019a-Python-3.6.8         (H)
NASM/.2.14.02-GCCcore-8.2.0                 (H)
x264/.20181203-GCCcore-8.2.0                (H)
LAME/.3.100-GCCcore-8.2.0                   (H)
x265/.3.0-GCCcore-8.2.0                     (H)
FriBidi/.1.0.5-GCCcore-8.2.0                (H)
FFmpeg/4.1.3-foss-2019a
libjpeg-turbo/.2.0.2-GCCcore-8.2.0          (H)
LibTIFF/.4.0.10-GCCcore-8.2.0               (H)
JasPer/.2.0.16-GCCcore-8.2.0                (H)
Java/1.8.0_202
ant/.1.10.5-Java-1.8                        (H)
gettext/.0.19.8.1-GCCcore-8.2.0             (H)
PCRE/.8.42-GCCcore-8.2.0                    (H)
GLib/.2.54.3-GCCcore-8.2.0                  (H)
ATK/.2.28.1-foss-2019a                      (H)
Gdk-Pixbuf/.2.36.12-foss-2019a              (H)
pixman/.0.38.0-GCCcore-8.2.0                (H)
cairo/.1.16.0-GCCcore-8.2.0                 (H)
HarfBuzz/.2.2.0-foss-2019a                  (H)
Pango/.1.42.4-foss-2019a                    (H)
GTK+/.2.24.32-foss-2019a                    (H)
OpenCV/4.0.1-foss-2019a-Python-3.6.8
NCCL/.2.4.2-CUDA-10.1.105                   (H)
PyTorch/1.1.0-foss-2019a-Python-3.6.8


title: mxnet to pytorch model

body:
I am trying to convert an mxnet Pre-trained model:
https://www.dropbox.com/s/akxeqp99jvsd6z7/model-MobileFaceNet-arcface-ms1m-refine-v1.zip?dl=0
I am trying to convert a pretrained model from mxnet to pytorch, but it always seems to fail. So, first I download, unzip the model files and run:
mmconvert -sf mxnet -in model-symbol.json -iw model-0000.params -df pytorch -om pytorch.pth --inputShape 3,112,112
and I get:
weight = self.weight_data.get(source_node.name + "_weight").asnumpy().transpose((1, 0))
AttributeError: 'NoneType' object has no attribute 'asnumpy'

which is the issue described here:
https://github.com/microsoft/MMdnn/issues/231
so, I changed the line 408 in mxnet_parser.py to:
weight = self.weight_data.get("fc1_weight").asnumpy().transpose((1, 0))
Now, I run again:
mmconvert -sf mxnet -in model-symbol.json -iw model-0000.params -df pytorch -om pytorch.pth --inputShape 3,112,112
and I get:
  File "pytorch.py", line 30, in __init__
    self.conv_2_dw_conv2d = self.__conv(2, name='conv_2_dw_conv2d', in_channels=64, out_channels=4096, kernel_size=(3, 3), stride=(1, 1), groups=64, bias=False)
  File "pytorch.py", line 335, in __conv
    layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['weights']))
RuntimeError: The size of tensor a (4096) must match the size of tensor b (64) at non-singleton dimension 0

I am not sure what it is trying to tell me other than that there seems a size mismatch. I was wondering if anyone has encountered this and have a solution for this?
Also, I get a warning during the conversion:
 UserWarning: You created Module with Module(..., label_names=['softmax_label']) but input with name 'softmax_label' is not found in symbol.list_arguments(). Did you mean one of:
    data
  warnings.warn(msg)

Is this error something I can safely ignore? Sorry, I am VERY new to MXNET.


title: Get all 2D diagonals of a 3D tensor in numpy

body:
I have a 3D tensor A x B x C. For each matrix B x C, I want to extract the leading diagonal.

Is there a vectorized way of doing this in numpy or pytorch instead of looping over A?


title: Where are the PyTorch python bindings to C++?

body:
I need to know, for a given function in the python api of PyTorch, how to find the corresponding C/++ code that it maps to underneath.

Better yet, if it is possible, for a given python function that is bound to some C/++ function in a loaded library, how to know the name of that c++ function for a given python function?


title: Pytorch on a fresh install of ubuntu can't find my GPU

body:
I have the latest ubuntu 18 installed just an hour ago. I have the newest anaconda 3.7 installed maybe 20 minutes ago. Then I proceeded to use

conda install pytorch torchvision -c pytorch

but when I open use python3 it still gives me

alex@alex:~$ python3
Python 3.7.0 (default, Jun 28 2018, 13:15:42)
[GCC 7.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available()
False
>>>

To be sure, I do have a gpu in my computer:

alex@alex:~$ lspci | grep VGA
01:00.0 VGA compatible controller: NVIDIA Corporation GP107 [GeForce GTX 1050 Ti] (rev a1)

I have also restarted my computer after the installing pytorch and it did not change anything. This is my second attempt at getting GPU to work on pytorch, my last attempt was on Lubuntu a few days ago using pip install and I installed some Nvidia drivers but still couldn't get it to work so i started anew.


title: DCGAN generator loss stuck at 0.7 after 5 epochs

body:
After playing around with the PyTorch DCGAN faces tutorial, i started working with my own dataset which consists of images with size 1x32x32 (channel, height, width).

Now, i did apply most of the things from this repository: https://github.com/soumith/ganhacks

But currently i am stuck.

I made this argument to choose whether to train the generator (G) or the discriminator (D).

    if i > 1:
        if D_G_z1 < 0.5:
            train_G = True
            train_D = False
        else:
            train_D = True
            train_G = False

Where i is the current batch number, train_D and train_G are set to True on batch one. D_G_z1 is D(G(x)).

I'd expect that once D is trained and D(G(x)) = 0.5, D will stop training and G will start training to improve the realism of the generated images, etc. Now D and G are training when the conditions are met.

However, the loss of G is stuck at 0.7 after 5 epochs and doesn't seem do change with 1k epochs (i havn't tried more). Changing the learning rate for G, or make G more/less complex by changing the amount of channels per ConvTranspose2d layer doesn't help either.

What's the best approach now? Any advice would be appreciated.

The code is found here: https://github.com/deKeijzer/SRON-DCGAN/blob/master/notebooks/ExoGAN_v1.ipynb

TLDR: Generator loss is stuck at 0.7, does't change anymore. Neither did it 'learn' a good representation of X.


title: How do I get the precursor nodes of each layer in Pytorch?

body:
I can get the summary of the model from pytorch, just like keras:

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
resnet = models.resnet18().to(device)

summary(resnet , (3, 224, 224))

result like this:

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 64, 112, 112]           9,408
       BatchNorm2d-2         [-1, 64, 112, 112]             128
              ReLU-3         [-1, 64, 112, 112]               0
         MaxPool2d-4           [-1, 64, 56, 56]               0
            Conv2d-5           [-1, 64, 56, 56]          36,864
       BatchNorm2d-6           [-1, 64, 56, 56]             128
              ReLU-7           [-1, 64, 56, 56]               0
            Conv2d-8           [-1, 64, 56, 56]          36,864
       BatchNorm2d-9           [-1, 64, 56, 56]             128
             ReLU-10           [-1, 64, 56, 56]               0
       BasicBlock-11           [-1, 64, 56, 56]               0
           Conv2d-12           [-1, 64, 56, 56]          36,864
      BatchNorm2d-13           [-1, 64, 56, 56]             128
             ReLU-14           [-1, 64, 56, 56]               0
           Conv2d-15           [-1, 64, 56, 56]          36,864
      BatchNorm2d-16           [-1, 64, 56, 56]             128
             ReLU-17           [-1, 64, 56, 56]               0
       BasicBlock-18           [-1, 64, 56, 56]               0
           Conv2d-19          [-1, 128, 28, 28]          73,728
      BatchNorm2d-20          [-1, 128, 28, 28]             256
             ReLU-21          [-1, 128, 28, 28]               0
           Conv2d-22          [-1, 128, 28, 28]         147,456
      BatchNorm2d-23          [-1, 128, 28, 28]             256
           Conv2d-24          [-1, 128, 28, 28]           8,192
      BatchNorm2d-25          [-1, 128, 28, 28]             256
             ReLU-26          [-1, 128, 28, 28]               0
       BasicBlock-27          [-1, 128, 28, 28]               0
           Conv2d-28          [-1, 128, 28, 28]         147,456
      BatchNorm2d-29          [-1, 128, 28, 28]             256
             ReLU-30          [-1, 128, 28, 28]               0
           Conv2d-31          [-1, 128, 28, 28]         147,456
      BatchNorm2d-32          [-1, 128, 28, 28]             256
             ReLU-33          [-1, 128, 28, 28]               0
       BasicBlock-34          [-1, 128, 28, 28]               0
           Conv2d-35          [-1, 256, 14, 14]         294,912
      BatchNorm2d-36          [-1, 256, 14, 14]             512
             ReLU-37          [-1, 256, 14, 14]               0
           Conv2d-38          [-1, 256, 14, 14]         589,824
      BatchNorm2d-39          [-1, 256, 14, 14]             512
           Conv2d-40          [-1, 256, 14, 14]          32,768
      BatchNorm2d-41          [-1, 256, 14, 14]             512
             ReLU-42          [-1, 256, 14, 14]               0
       BasicBlock-43          [-1, 256, 14, 14]               0
           Conv2d-44          [-1, 256, 14, 14]         589,824
      BatchNorm2d-45          [-1, 256, 14, 14]             512
             ReLU-46          [-1, 256, 14, 14]               0
           Conv2d-47          [-1, 256, 14, 14]         589,824
      BatchNorm2d-48          [-1, 256, 14, 14]             512
             ReLU-49          [-1, 256, 14, 14]               0
       BasicBlock-50          [-1, 256, 14, 14]               0
           Conv2d-51            [-1, 512, 7, 7]       1,179,648
      BatchNorm2d-52            [-1, 512, 7, 7]           1,024
             ReLU-53            [-1, 512, 7, 7]               0
           Conv2d-54            [-1, 512, 7, 7]       2,359,296
      BatchNorm2d-55            [-1, 512, 7, 7]           1,024
           Conv2d-56            [-1, 512, 7, 7]         131,072
      BatchNorm2d-57            [-1, 512, 7, 7]           1,024
             ReLU-58            [-1, 512, 7, 7]               0
       BasicBlock-59            [-1, 512, 7, 7]               0
           Conv2d-60            [-1, 512, 7, 7]       2,359,296
      BatchNorm2d-61            [-1, 512, 7, 7]           1,024
             ReLU-62            [-1, 512, 7, 7]               0
           Conv2d-63            [-1, 512, 7, 7]       2,359,296
      BatchNorm2d-64            [-1, 512, 7, 7]           1,024
             ReLU-65            [-1, 512, 7, 7]               0
       BasicBlock-66            [-1, 512, 7, 7]               0
        AvgPool2d-67            [-1, 512, 1, 1]               0
           Linear-68                 [-1, 1000]         513,000
================================================================

But in keras, I am able to get the precursor nodes of each layer.

Model Summary:
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to
====================================================================================================
input_1 (InputLayer)             (None, 1, 15, 27)     0
____________________________________________________________________________________________________
convolution2d_1 (Convolution2D)  (None, 8, 15, 27)     872         input_1[0][0]
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D)    (None, 8, 7, 27)      0           convolution2d_1[0][0]
____________________________________________________________________________________________________
flatten_1 (Flatten)              (None, 1512)          0           maxpooling2d_1[0][0]
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 1)             1513        flatten_1[0][0]
====================================================================================================

How do I get the precursor nodes of each layer in pytorch? I looked at OrderDict, which has no information about the precursor nodes.
How can I get information about each layer of precursor nodes in pytorch?


title: I have pytorch installed in a environment but import torch produces an error

body:
I have anaconda python3 kernel with pytorch and numpy installed in the environment. In jupyter notebook first line 'import torch' produces error.

I am using anaconda navigator to launch jupyter notebook and enter my environment and see pytorch is installed but not being imported. Tried various dir extensions 'from torch... import * but more error

import torch

ImportError                               Traceback (most recent call last)
<ipython-input-1-20507c95d9af> in <module>
      1
----> 2 import torch
      3
      4

~/anaconda3/envs/udacity1/lib/python3.6/site-packages/torch/__init__.py in <module>
    100     pass
    101
--> 102 from torch._C import *
    103
    104 __all__ += [name for name in dir(_C)

ImportError: /home/frida/anaconda3/envs/udacity1/lib/python3.6/site-packages/torch/lib/libtorch.so.1: undefined symbol: nvrtcGetProgramLogSize


title: How to convert numpy.ndarray to scipy.sparse.coo_matrix

body:
The error I'm getting is:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

I saw some questions about it .any() or .all(), but didn't solve my problem. I built a sparse block diagonal matrix, by using scipy.linalg.block_diag, as I need to input torch.sparse tensor to my model. And (as far as I know) the only way for creating torch.sparse tensor is using scipy.sparse.coo_matrix. However, after building the block diagonal matrix, it automatically becomes numpy.ndarray, and torch.sparse doesn't take this input. I've tried many things, but couldn't handle. (I've tried many pytorch sparse tensor builder library, or code also, but didn't work.)

I have tried to convert back to scipy, and it worked for this toy matrix:

from scipy.linalg import block_diag

a = [2]
b = [3,4]
x = block_diag(a,b)
print(x)
print(type(x))
s = sp.coo_matrix(x, dtype = sp.coo_matrix)
print(s)
print(type(s))

Output:

 [[2 0 0]
 [0 3 4]]
<class 'numpy.ndarray'>
  (0, 0)    2
  (1, 1)    3
  (1, 2)    4
<class 'scipy.sparse.coo.coo_matrix'>

But when I try with my real matrix, it gives me "Boolean" Value Error.

sparse = sp.coo_matrix(top5_block.any(), dtype = sp.coo_matrix)

Error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

I have tried .all() and .any() (if i did correctly), both of them gives the same error. If I don't use them, still gives the same error. For sparse tensor I need sparse block diagonal matrix(with hybrid sparse tensor I also tried, but my model didn't work.), but it is backfiring me.
My sparse block diagonal matrix has a size of 50x50, and each of those 50 has different size of another square matrix(as it is block diagonal).

[[<183x183 sparse matrix of type '<class 'numpy.float64'>'
    with 8043 stored elements in Compressed Sparse Row format>
  0 0 ... 0 0 0]
 [0
  <169x169 sparse matrix of type '<class 'numpy.float64'>'
    with 4081 stored elements in Compressed Sparse Row format>
  0 ... 0 0 0]
 [0 0
  <188x188 sparse matrix of type '<class 'numpy.float64'>'
    with 5996 stored elements in Compressed Sparse Row format>
  ... 0 0 0]
 ...
 [0 0 0 ...
  <157x157 sparse matrix of type '<class 'numpy.float64'>'
    with 1513 stored elements in Compressed Sparse Row format>
  0 0]
 [0 0 0 ... 0
  <121x121 sparse matrix of type '<class 'numpy.float64'>'
    with 1373 stored elements in Compressed Sparse Row format>
  0]
 [0 0 0 ... 0 0
  <262x262 sparse matrix of type '<class 'numpy.float64'>'
    with 5890 stored elements in Compressed Sparse Row format>]]
(50, 50)

Any help to convert this block diagonal matrix back to scipy.sparse would be appreciated.

EDIT:
This is how I obtained sparse diagonal matrix(Or at least I thought):

def build_block_diag(matrice_list):
    return block_diag(*matrice_list)

top5_block = build_block_diag(mat_list)


title: RuntimeError: value cannot be converted to type uint8_t without overflow: -0.192746

body:
I am new to Pytorch and am aiming to do an image classification task using a CNN based on the EMNIST dataset.
I read my data in as follows:
emnist = scipy.io.loadmat(DATA_DIR + '/emnist-letters.mat')
data = emnist ['dataset']
X_train = data ['train'][0, 0]['images'][0, 0]
X_train = X_train.reshape((-1,28,28), order='F')

y_train = data ['train'][0, 0]['labels'][0, 0]

X_test = data ['test'][0, 0]['images'][0, 0]
X_test = X_test.reshape((-1,28,28), order = 'F')

y_test = data ['test'][0, 0]['labels'][0, 0]

train_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_train), torch.from_numpy(y_train))
test_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_test), torch.from_numpy(y_test))

batch_size = 128
n_iters = 3000
num_epochs = n_iters / (len(train_dataset) / batch_size)
num_epochs = int(num_epochs)

train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=False)

Then, I found the following configurations (that I still have to adjust to fit to my data):
class CNNModel(nn.Module):
    def __init__(self):
        super(CNNModel, self).__init__()

        # Convolution 1
        self.cnn1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=0)
        self.relu1 = nn.ReLU()

        # Max pool 1
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)

        # Convolution 2
        self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=0)
        self.relu2 = nn.ReLU()

        # Max pool 2
        self.maxpool2 = nn.MaxPool2d(kernel_size=2)

        # Fully connected 1 (readout)
        self.fc1 = nn.Linear(32 * 4 * 4, 10)

    def forward(self, x):
        # Convolution 1
        out = self.cnn1(x)
        out = self.relu1(out)

        # Max pool 1
        out = self.maxpool1(out)

        # Convolution 2
        out = self.cnn2(out)
        out = self.relu2(out)

        # Max pool 2
        out = self.maxpool2(out)

        # Resize
        # Original size: (100, 32, 7, 7)
        # out.size(0): 100
        # New out size: (100, 32*7*7)
        out = out.view(out.size(0), -1)

        # Linear function (readout)
        out = self.fc1(out)

        return out

model = CNNModel()

criterion = nn.CrossEntropyLoss()

To train the model, I use the following code:
iter = 0
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):

        # Add a single channel dimension
        # From: [batch_size, height, width]
        # To: [batch_size, 1, height, width]
        images = images.unsqueeze(1)

        # Forward pass to get output/logits
        outputs = model(images)

        # Clear gradients w.r.t. parameters
        optimizer.zero_grad()

        # Forward pass to get output/logits
        outputs = model(images)

        # Calculate Loss: softmax --> cross entropy loss
        loss = criterion(outputs, labels)

        # Getting gradients w.r.t. parameters
        loss.backward()

        # Updating parameters
        optimizer.step()

        iter += 1

        if iter % 500 == 0:
            # Calculate Accuracy
            correct = 0
            total = 0
            # Iterate through test dataset
            for images, labels in test_loader:

                images = images.unsqueeze(1)

                # Forward pass only to get logits/output
                outputs = model(images)

                # Get predictions from the maximum value
                _, predicted = torch.max(outputs.data, 1)

                # Total number of labels
                total += labels.size(0)

                correct += (predicted == labels).sum()

            accuracy = 100 * correct / total

            # Print Loss
            print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter, loss.data[0], accuracy))

However, when I run this, I get the following error:
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-27-1fbdd53d1194> in <module>()
     12
     13         # Forward pass to get output/logits
---> 14         outputs = model(images)
     15
     16         # Clear gradients w.r.t. parameters

4 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
    348                             _pair(0), self.dilation, self.groups)
    349         return F.conv2d(input, weight, self.bias, self.stride,
--> 350                         self.padding, self.dilation, self.groups)
    351
    352     def forward(self, input):

RuntimeError: value cannot be converted to type uint8_t without overflow: -0.0510302

I found this question already and think that the solution might work for me as well. However, I don't understand where in my code I can implement this.
What can I do to overcome this problem?
Ps.
I have used the following import statements:
    import scipy .io
    import torch
    import torch.nn as nn
    import torchvision.transforms as transforms
    import torchvision.datasets as dsets
    from torch.autograd import Variable
    import cv2
    import torch
    import torch.nn as nn
    import torch.optim as optim
    import torch.nn.functional as F
    import numpy as np
    import os
    from PIL import Image
    from PIL import ImageOps
    from torchvision import datasets, transforms
    from torch.autograd import Variable
    import matplotlib.pyplot as plt
    from torchvision.datasets import ImageFolder
    from torch.utils.data import DataLoader
    from torchvision.transforms import ToTensor
    from torch.nn import Sequential
    from torch.nn import Conv2d
    from torch.nn import BatchNorm2d
    from torch.nn import MaxPool2d
    from torch.nn import ReLU
    from torch.nn import Linear


title: How to use with torch.cuda.device() conditionally

body:
I have some code encapsulated in a:
with torch.cuda.device(self.device):
    # do a bunch of stuff

And in my __init__ I have:
self.device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')

But I'm a little confused about how to deal with a situation where the device is cpu. Because torch.cuda.device is already explicitly for cuda. Should I just write a decorator for the function? Seems a bit overkill


title: OSError: [Errno 22] Invalid argument when using torch.load

body:
I am trying to load my dataset and it was working before but all of the sudden this error started pooping up.
When I try to load it like this:
  train_set = Database_load(root = "C:\\Users\\Public\\PhysNet\\",train = "train.pth")

It gives me the following error:

  File "C:\Users\Public\Lucas\PhysNet\modt.py", line 185, in <module>
    train_set = Database_load(root = "C:\\Users\\Public\\Lucas\\PhysNet\\",train = "train.pth")

  File "C:\Users\Public\Lucas\PhysNet\database_load.py", line 21, in __init__
    self.data, self.y1, self.y2= torch.load(os.path.join(self.root, self.train))

  File "C:\ProgramData\Anaconda3\envs\project\lib\site-packages\torch\serialization.py", line 386, in load
    return _load(f, map_location, pickle_module, **pickle_load_args)

  File "C:\ProgramData\Anaconda3\envs\project\lib\site-packages\torch\serialization.py", line 580, in _load
    deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)

OSError: [Errno 22] Invalid argument

I have tried doing the following as well when entering the path but it has not worked:
  train_set = Database_load(root = r"C:\\Users\\Public\\PhysNet\\",train = "train.pth")

  train_set = Database_load(root = r'C:/Users/Public/PhysNet/',train = 'train.pth')

Any suggestions on how to fix this issue?


title: Full Page Text Recognition Dataset Creation

body:
I have been reading OCR papers such as this one https://arxiv.org/pdf/1704.08628.pdf , and I am have trouble finding out how these datasets are actually generated.

In the linked paper, they use a regressor to predict the start location (a point) and height of a line of text. Then, based on that starting point and height, a second network performs OCR and end of line detection. I realize this is a very simplified explanation, but it follows that their dataset consists (at least in part) of full page text 'images' annotated with where each line begins, and then a transcription of the text on a given line. Alternatively, they could have just used the lower left point of bounding boxes as the start point and the height of the box as the word height (avoiding the need to re-annotate if the data was previously prepared using bounding boxes).

So how is a dataset like this actually created? Looking at other datasets it seems like there is some software that can create XML files containing the ground truths relevant to each image, can someone point me in the right direction? I've been googling around and finding lots of tools for annotating text with sentiment etc and other tools for annotating images for segmentation (for something like a YOLO network), but I'm coming up empty for the creation something like the Maurdoor dataset used in the linked paper.

Thank you


title: Matrix Factorization with PyTorch using GPU

body:
The below code does run, but it's very slow as it's using a for loops. At my university, servers with GPU resources are available. Likewise, I'd like to understand how to use batches to train the model more effectively.

import torch
import torch.nn as nn
import torch.nn.functional as F

class MatrixFactorization(torch.nn.Module):
    def __init__(self, n_items=len(movie_ids), n_factors=300):
        super().__init__()

        self.vectors = nn.Embedding(n_items, n_factors,sparse=True)

    def forward(self, i,j):
        feat_i = self.vectors(i)
        feat_j = self.vectors(j)
        result = (feat_i * feat_j).sum(-1)
        return result

model = MatrixFactorization(n_items= len(movie_ids),n_factors=300)
loss_fn = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

epochs = 100
for epoch in range(epochs):
    loss = 0
    for r,c in zip(r_index, c_index):
        i = torch.LongTensor([int(r)])
        j = torch.LongTensor([int(c)])
        rating = torch.FloatTensor([Xij[i, j]])
        # predict
        prediction = model(i, j)
        loss += loss_fn(prediction, rating)

                # Reset the gradients to 0
    optimizer.zero_grad()

                # backpropagate
    loss.backward()

                # update weights
    optimizer.step()
    print(loss)

I've tried the below alteration but it produced a warning. I'm not sure why my target sizes are mismatched, but that appears to be the cause of the issue.

epochs = 50
for epoch in range(epochs):
    loss = 0

    # predict
    i = torch.LongTensor(r_index)
    j = torch.LongTensor(c_index)
    ratings = Xij[i, j]
    prediction = model(i, j)
    loss += loss_fn(prediction, rating)

                # Reset the gradients to 0
    optimizer.zero_grad()

                # backpropagate
    loss.backward()

                # update weights
    optimizer.step()
    print(loss)

And the warning (not sure where I went wrong):

/anaconda3/lib/python3.6/site-packages/torch/nn/modules/loss.py:431: UserWarning: Using a target size (torch.Size([1])) that is different to the input size (torch.Size([5931640])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)


title: No module named 'custom_transforms'

body:
I am working on a project (spatio-temporal video segmentation) in which I am using the COSNET Model written in pytorch. While trying to run the model on google colab (specifically the part below) I get the error:

  No module named custom_transforms

I tried the !pip install custom_transforms but I get:

  ERROR: Could not find a version that satisfies the requirement
  custom_transforms (from versions: none) ERROR: No matching distribution found for custom_transforms

if __name__ == '__main__':
    import custom_transforms as tr
    import torch
    from torchvision import transforms
    from matplotlib import pyplot as plt

    transforms = transforms.Compose([tr.RandomHorizontalFlip(), tr.Resize(scales=[0.5, 0.8, 1]), tr.ToTensor()])


title: How to get indices of multiple elements in a 2D tensor, in a GPU friendly way?

body:
This question is similar to that already answered here, but that question does not address how to retrieve the indices of multiple elements.

I have a 2D tensor points with many rows and a small number of columns, and would like to get a tensor containing the row indices of all the elements in that tensor. I know what elements are present in points beforehand; It contains integer elements ranging from 0 to 999, and I can make a tensor using the range function to reflect the set of possible elements. The elements may be in any of the columns.

How can I retrieve the row indices where each element appears in my tensor in a way that avoids looping or using numpy, so I can do this quickly on a GPU?

I am looking for something like (points == elements).nonzero()[:,1]

Thanks!


title: Sampling data batch wise from tensor Pytorch

body:
I have train_x and valid_x splited from trainX ,train_y and valid_y splited from trainY and they are having shapes as per below. i want to classify images of labels LABELS = set(["Faces", "Leopards", "Motorbikes", "airplanes"]).
print(train_x.shape, len(train_y))
torch.Size([1339, 96, 96, 3]) 1339

print(valid_x.shape, len(valid_y))
torch.Size([335, 96, 96, 3]) 335

print(testX.shape, len(testY))
torch.Size([559, 96, 96, 3]) 559

so i want to use regular train/valid on data batch-wise code as per below :
#train the network

n_epochs = 20

valid_loss = []
train_loss = []

for epoch in range(1,n_epochs+1):

    cur_train_loss = 0.0
    cur_valid_loss = 0.0

    #####################
    #### Train model ####
    #####################
    cnn_model.train()

    for data, target in trainLoader:
        if train_on_gpu:
            data, target = data.cuda(), target.cuda()
        optimizer.zero_grad()
        output = cnn_model(data)
        loss = criterion(output, target)
        loss.backward()
        optimizer.step()
        cur_train_loss += loss.item() * data.size(0)

    ########################
    #### Validate model ####
    ########################

    cnn_model.eval()

    for data, target in validLoader:
        if train_on_gpu:
            data, target = data.cuda(), target.cuda()
        output = cnn_model(data)
        loss = criterion(output, target)
        cur_valid_loss += loss.item() * data.size(0)

    # calculate avg loss
    avg_train_loss = cur_train_loss / len(trainLoader.sampler)
    avg_valid_loss = cur_valid_loss / len(validLoader.sampler)

    train_loss.append(avg_train_loss)
    valid_loss.append(avg_valid_loss)

    print('Epoch: {} \t train_loss: {:.6f} \t valid_loss: {:.6f}'.format(epoch, avg_train_loss, avg_valid_loss))

so what i have to do for that ?
i have search for that but nothing specific i found out. i want to use pytorch for this. i have built model for another problem same like this but in that i have used DataLoader for loading one batch of data at a time for training and validation.


title: How to check whether tensor values in a different tensor pytorch?

body:
I have 2 tensors of unequal size
a = torch.tensor([[1,2], [2,3],[3,4]])
b = torch.tensor([[4,5],[2,3]])

I want a boolean array of whether each value exists in the other tensor without iterating. something like
a in b

and the result should be
[False, True, False]

as only the value of a[1] is in b


title: Convert from keras to pytorch

body:
I am training a multilabel classifier in keras ,i want to convert that to pytorch , i am mostly confused about how to handle the loss ,this is what the code looks like

model = keras.applications.densenet.DenseNet121(include_top=False, input_shape=(224, 224, 3))
x = model.output
x = Flatten()(x)
x = Dense(512)(x)
x = Activation('relu')(x)
x = Dropout(0.5)(x)
output1 = Dense(1, activation = 'sigmoid')(x)
output2 = Dense(1, activation = 'sigmoid')(x)
output3 = Dense(1, activation = 'sigmoid')(x)
output4 = Dense(1, activation = 'sigmoid')(x)
output5 = Dense(1, activation = 'sigmoid')(x)
output6 = Dense(1, activation = 'sigmoid')(x)
output7 = Dense(1, activation = 'sigmoid')(x)
output8 = Dense(1, activation = 'sigmoid')(x)
model = Model(model.inputs,[output1,output2,output3,output4,output5, output6, output7, output8])
# print(model.summary())
model.compile(optimizers.rmsprop(lr = 0.0001, decay = 1e-6),
loss = ["binary_crossentropy","binary_crossentropy","binary_crossentropy","binary_crossentropy", "binary_crossentropy","binary_crossentropy","binary_crossentropy","binary_crossentropy"],metrics = ["accuracy"])

How can i do this in pytorch ,this is what i have till now

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, kernel_size=3)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(2304, 256)
        self.fc2 = nn.Linear(256, 8)

    def forward(self, x):

        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(x.size(0), -1) # Flatten layer
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.sigmoid(x)

model = Net().cuda()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)

def train(epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        # data, target = data.cuda(async=True), target.cuda(async=True) # On GPU

        data, target = Variable(data).cuda(), Variable(target).float().cuda()
        optimizer.zero_grad()
        output = model(data)

        loss = F.binary_cross_entropy(output, target)
        loss.backward()
        optimizer.step()
        if batch_idx % 10 == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))

for epoch in range(1, 10):
    train(epoch)

Thanks in advance, any suggestions will be very helpful


title: Using TorchScript classes as members in pytorch modules

body:
I'm trying to make some existing pytorch models support the TorchScript jit compiler, but I'm running into issues with members that are of non-primitive types.

This small example illustrates the issue:

import torch

@torch.jit.script
class Factory(object):
    def __init__(self):
        pass

    def create(self, x: float) -> torch.Tensor:
        return torch.tensor([x])

class Foo(torch.nn.Module):
    def __init__(self):
        super(Foo, self).__init__()
        self.factory: Factory = Factory()

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.factory.create(0)

mod = torch.jit.script(Foo())

When ran, the jit compiler gives the error

RuntimeError:
module has no attribute 'factory':
at example.py:17:15
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.factory.create(0)
               ~~~~~~~~~~~~ <--- HERE

I've tested that the Factory class is available to the jit inside the forward method, but it does not acknowledge it when I store it as a member. Why is this? And is there any way I can get the jit compiler to save this kind of members into the compiled module?


title: How Can I change the value of "maxdets" in Faster R-CNN by Pytorch?

body:
I am implementing a faster RCNN network on pytorch. I have followed the next tutorial.

https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html

There are images in which I have more than 100 objects to classify. However, with this tutorial I can only detect a maximum of 100 objects, since the parameter "maxdets" = 100.

Is there a way to change this value to adapt it to my project?

IoU metric: bbox
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.235
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.655
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.105
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.238
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.006
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.066
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.331
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.331
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000

If only change next param, it would be the problem solved?

cocoeval.Params.setDetParams.maxDets = [1, 10, 100]

Thank you!


title: PyTorch model prediction fail for single item

body:
I use PyTorch and transfer learning to train mobilenet_v2 based classifier. I use a batch of 20 images during training and my test accuracy is ~80%.
I try to use the model with single image for individual prediction and output is a wrong class.
At the same time if I will take a batch from my test dataset and insert my single image in it instead of element 0 it will have a correct prediction. Prediction 0 will be a correct class. So model works for a batch but not for an individual item.
If I just repeat my single image and pass 10 copies of it the result prediction still will be wrong. So I have situation when my prediction accuracy somehow depends on other items in the batch.
My code for individual item test:
from PIL import Image

# load one batch from test set (20)
dataiter = iter(test_loader)
images, labels = dataiter.next()

# load image we want get prediction for
img_path = "dataset/Barber50/Barber50-25r.jpg"
image = Image.open(img_path)
image_cropped = transforms.CenterCrop(img_size)(transforms.Resize(img_size)(image))
image_tensor = transforms.Normalize(mean=mean, std=std)(transforms.ToTensor()(image_cropped))

# insert the image into batch from test set
images[0] = image_tensor

# show image
fig = plt.figure(figsize=(5, 5))
inp = images[0].numpy().transpose((1, 2, 0))
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.imshow(inp)

# mode data to gpu, switch to eval
if train_on_gpu: images = images.cuda()
model.eval()

# get prediction. PROBLEM HERE
output = model(images[:1]) # predicts wrong class if :1 and correct class if :10

# turn prediction into class label
output = F.softmax(output, dim=1)
_, pred = torch.max(output, 1)
print(classes[pred[0]])


title: Why is the device parameter of torch.cuda.device probably greater than torch.cuda.device_count()?

body:
It's the code:

torch.cuda.device_count()
# output: 1
d2 = torch.cuda.device(2)
# d2.idx = 2

Is it for the model running on other device?


title: Pytorch: How to create a custom Dataset with reference table

body:
I have a reference.csv file that has three columns: Type, Class, and Path. Here are the first 5 example rows:

"Type","Class","Path"
"train","A","./path1/001.jpg"
"train","A","./path2/002.jpg"
"test","C","./path3/003.jpg"
"train","B","./path4/001.jpg"
"test","B","./path5/002.jpg"
...

In a more viewer friendly format:

|----------------------|------------------|------------------|
|         Type         |       Class      |       Path       |
|----------------------|------------------|------------------|
|        train         |         A        | ./path1/001.jpg  |
|----------------------|------------------|------------------|
|        train         |         A        | ./path2/002.jpg  |
|----------------------|------------------|------------------|
|        train         |         C        | ./path3/003.jpg  |
|----------------------|------------------|------------------|
|        test          |         B        | ./path4/001.jpg  |
|----------------------|------------------|------------------|
|        test          |         B        | ./path5/002.jpg  |
|----------------------|------------------|------------------|

I want to create a Dataset class (torch.utils.data.Dataset) to read the images so I can use the DataLoader (torch.utils.data.DataLoader).

What is the correct way to create a custom Dataset using a reference table?


title: Runtime error using Python Library Keops using CUDA in Ubuntu18.04

body:
I am trying to run samples from the Python library: GeomLoss, which depends on CUDA, Pytorch and Keops in Ubuntu 18.04.3. I downloaded Python3.7 using Anaconda, and I am using CUDA 10.1. The gcc version is 7.4.0.

When I run samples from GeomLoss, the error message said:

  RuntimeError: [KeOps] This KeOps shared object has been compiled
  without cuda support: try to set tagHostDevice to 0 or recompile the
  formula with a working version of cuda.

I cannot change tagHostDevice to 0 since this will disable GPU calculation according to their documentation. I checked CUDA and Pytorch installation and they was no error.
But when I tried to run the installation checking code from KeOps:

import torch
import pykeops.torch as pktorch

x = torch.arange(1, 10, dtype=torch.float32).view(-1, 3)
y = torch.arange(3, 9, dtype=torch.float32).view(-1, 3)

my_conv = pktorch.Genred('SqNorm2(x-y)', ['x = Vi(3)', 'y = Vj(3)'])
print(my_conv(x, y))

I received error message:

  error -- unsupported GNU version! gcc versions later than 6 are not supported! ^~~~~ CMake Error at
  keopslibKeOpstorch91c92bd508_generated_link_autodiff.cu.o.Release.cmake:219

I checked CUDA documentation, for Ubuntu 18.04.3, the native linux distribution support should be gcc-7.3.0. for x86_64. I used gcc --version to check default gcc in system and it is using gcc-7.4.0. I am not sure if this is the problem with using KeOps with CUDA and GPU. Also, I believe KeOps will not support gcc versions before 7. So I am really confused about what should I do to fix the problem right now.

I am wondering if anyone has experienced similar problems with GeomLoss and KeOps or other libraries. I am indeed grateful for any suggestions. Thanks!


title: Derived Class of Pytorch nn.Module Cannot be Loaded by Module Import in Python

body:
Using Python 3.6 with Pytorch 1.3.1. I have noticed that some saved nn.Modules cannot be loaded when the whole module is being imported into another module. To give an example, here is the template of a minimum working example.

#!/usr/bin/env python3
#encoding:utf-8
# file 'dnn_predict.py'

from torch import nn
class NN(nn.Module):##NN network
    # Initialisation and other class methods

networks=[torch.load(f=os.path.join(resource_directory, 'nn-classify-cpu_{fold}.pkl'.format(fold=fold))) for fold in range(5)]
...
if __name__=='__main__':
    # Some testing snippets
    pass

The whole file works just fine when I run it in the shell directly. However, when I want to use the class and load the neural network in another file using this code, it fails.

#!/usr/bin/env python3
#encoding:utf-8
from dnn_predict import *

The error reads AttributeError: Can't get attribute 'NN' on <module '__main__'>

Does loading of saved variables or importing modules happen differently in Pytorch than other common Python libraries? Some help or pointer to the root cause will be really appreciated.


I face that error
RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15____
My input is binary vector of 340, target is binary vector of 8, For '"
loss = criterion(outputs, stat_batch), I got outputs.shape= [64,8] and stat_batch.shape=[64,8]
Here is the model
class MMP(nn.Module):

    def __init__(self, M=1):
        super(MMP, self).__init__()
        # input layer
        self.layer1 = nn.Sequential(
            nn.Conv1d(340, 256,  kernel_size=1, stride=1, padding=0),
            nn.ReLU())
        self.layer2 = nn.Sequential(
            nn.Conv1d(256, 128, kernel_size=1, stride=1, padding=0),
            nn.ReLU())
        self.layer3 = nn.Sequential(
            nn.Conv1d(128, 64, kernel_size=1, stride=1, padding=0),
            nn.ReLU())
        self.drop1 = nn.Sequential(nn.Dropout())
        self.batch1 = nn.BatchNorm1d(128)
        # LSTM
        self.lstm1=nn.Sequential(nn.LSTM(
        input_size=64,
        hidden_size=128,
        num_layers=2,
        bidirectional=True,
        batch_first= True))
        self.fc1 = nn.Linear(128*2,8)
        self.sof = nn.Softmax(dim=-1)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.drop1(out)
        out = out.squeeze()
        out = out.unsqueeze(0)
        #out = out.batch1(out)
        out,_ = self.lstm1(out)
        print("lstm",out.shape)
        out = self.fc1(out)
        out =out.squeeze()
        #out = out.squeeze()
        out = self.sof(out)
        return out

#traiin_model
criterion = nn.CrossEntropyLoss()
if CUDA:
    criterion = criterion.cuda()
optimizer = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=0.9)

for epoch in range(N_EPOCHES):
    tot_loss=0
    # Training
    for i, (seq_batch, stat_batch) in enumerate(training_generator):
        # Transfer to GPU
        seq_batch, stat_batch = seq_batch.to(device), stat_batch.to(device)
        print(i)
        print(seq_batch)
        print(stat_batch)
        optimizer.zero_grad()
        # Model computation
        seq_batch = seq_batch.unsqueeze(-1)
        outputs = model(seq_batch)
        if CUDA:
            loss = criterion(outputs, stat_batch).float().cuda()
        else:
            loss = criterion(outputs.view(-1), stat_batch.view(-1))
        print(f"Epoch: {epoch},number: {i}, loss:{loss.item()}...\n\n")

        tot_loss += loss.item(print(f"Epoch: {epoch},file_number: {i}, loss:{loss.item()}...\n\n"))
        loss.backward()
        optimizer.step()


title: F.relu(self.fc1(x)) is causing RuntimeError problem

body:
I have implemented the following CNN for my training and validation data sets
that contain 90 and 20 images respectively divided into 3 classes:

def __init__(self):
    super(Net, self).__init__()
    self.conv1 = nn.Conv2d(3, 6, 5)
    self.pool = nn.MaxPool2d(2, 2)
    self.conv2 = nn.Conv2d(6, 16, 5)
    self.fc1 = nn.Linear(16 * 5 * 5, 120)
    self.fc2 = nn.Linear(120, 50)
    self.fc3 = nn.Linear(50, len(classes))

def forward(self, x):
    print(x.shape)
    x = self.pool(F.relu(self.conv1(x)))
    print(x.shape)
    x = self.pool(F.relu(self.conv2(x)))
    print(x.shape)
    x = x.view(x.size(0),-1)
        #x = x.view(-1,x.size(1)*x.size(2)*x.size(3))
        #x = x.view(-1, 16 * 5 * 5)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x

I am getting the following error while running:

RuntimeError: size mismatch, m1: [1 x 214720], m2: [400 x 120] at /opt/conda/conda-bld/pytorch_1524584710464/work/aten/src/TH/generic/THTensorMath.c:2033 for x = F.relu(self.fc1(x))

Can someone please suggest what I should do to get rid of this problem? I changed the x.view(...) by following some threads. However, it did not help in this case.


title: Traceback (most recent call last) in Colab when looping through dataloader in pytorch

body:
I'm working on a project to classify flower images using a pre-trained model vgg19 using pytorch.

I'm relying on the model features only and using a custom classifier.

However on starting a for-loop to feed images to the model classifier and calculate accuracy through epochs I get an error.

I'm not sure what's the problem as the error is a traceback (most recent call last)

Below is my notebook.

The cell throwing the error is below

#training the classifier
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.classifier.parameters(),lr=0.01)

steps = 0
running_loss = 0
epochs = 5
print_every = 5

for epoch in range(epochs):
    for images,labels in train_dataloader:
        steps += 1
        optimizer.zero_grad()

        logps = model.forward(images)
        loss  = criterion(logps,labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()

        if steps % print_every == 0:
            test_loss = 0
            accuracy = 0
            model.eval()

            with torch.no_grad():
                for images, labels in valid_dataloader:
                    logps = model.forward(images)
                    batch_loss = criterion(logps, labels)

                    test_loss += batch_loss.item()

                    #Calculate accuracy
                    ps = torch.exp(logps)
                    top_p, top_class = ps.topk(5,dim=1)
                    equals = top_class  == labels.view(*top_class.shape)
                    accuracy += torch.mean(equals.type(torch.FloatTensor)).item()

            print(f"Epoch {epoch+1}/{epochs}.."
             f"Train loss: {running_loss/print_every: .3f}.."
             f"Test loss: {test_loss/len(valid_loader):.3f}.."
             f"Test accuracy: {accuracy/len(valid_loader):.3f}")
            running_loss = 0
            model.train()

The error I get on running the notebook

AttributeError                            Traceback (most recent call last)
<ipython-input-11-c218f8f2b72e> in <module>()
      8
      9 for epoch in range(epochs):
---> 10     for images,labels in train_dataloader:
     11         steps += 1
     12         optimizer.zero_grad()

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    312         if self.num_workers == 0:  # same-process loading
    313             indices = next(self.sample_iter)  # may raise StopIteration
--> 314             batch = self.collate_fn([self.dataset[i] for i in indices])
    315             if self.pin_memory:
    316                 batch = pin_memory_batch(batch)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in <listcomp>(.0)
    312         if self.num_workers == 0:  # same-process loading
    313             indices = next(self.sample_iter)  # may raise StopIteration
--> 314             batch = self.collate_fn([self.dataset[i] for i in indices])
    315             if self.pin_memory:
    316                 batch = pin_memory_batch(batch)

/usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in __getitem__(self, index)
     99         """
    100         path, target = self.samples[index]
--> 101         sample = self.loader(path)
    102         if self.transform is not None:
    103             sample = self.transform(sample)

/usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in default_loader(path)
    145         return accimage_loader(path)
    146     else:
--> 147         return pil_loader(path)
    148
    149

/usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in pil_loader(path)
    127     # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    128     with open(path, 'rb') as f:
--> 129         img = Image.open(f)
    130         return img.convert('RGB')
    131

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
   2319     return True
   2320
-> 2321
   2322 def new(mode, size, color=0):
   2323     """

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in preinit()
    368
    369
--> 370 def preinit():
    371     """Explicitly load standard file format drivers."""
    372

/usr/local/lib/python3.6/dist-packages/PIL/PpmImagePlugin.py in <module>()
    156 Image.register_save(PpmImageFile.format, _save)
    157
--> 158 Image.register_extensions(PpmImageFile.format, [".pbm", ".pgm", ".ppm"])

AttributeError: module 'PIL.Image' has no attribute 'register_extensions'


title: How to make the convolution in pytorch associative?

body:
The discrete convolution is by definition associative. But when I try to verify this in pytorch, I can not find get a plausible result.

The associative law is $f*(g*\psi)=(f * g)*\psi$, so I create three discrete functions centered at zero(as tensors) and convolve them with proper zero paddings so that all non-zero element in result map is obtained.

import torch
import torch.nn as nn

def test_conv_compst():
    # $\psi$
    inputs = torch.randn((1,4,7,7))
    # $g$
    a = torch.randn((7, 4, 3, 3))
    # $f$
    b = torch.randn((3, 7, 3, 3))
    int_1 = torch.conv2d(inputs, a, padding=2)
    # results obtained by the first order
    res_1 = torch.conv2d(int_1, b, padding=2)

    comp_k = torch.conv2d(a.transpose(1, 0), b, padding=2).transpose(1, 0)
    print(comp_k.shape)
    # results obtained through the second order
    res_2 = torch.conv2d(inputs, comp_k, padding=4)
    print(res_1.shape)
    print(res_2.shape)
    print(torch.max(torch.abs(res_2-res_1)))

The expected result is that the difference from the two results is negligible. But it returns:

torch.Size([3, 4, 5, 5])
torch.Size([1, 3, 11, 11])
torch.Size([1, 3, 11, 11])
tensor(164.8044)


title: What is the difference between zero dimensional tensor and item?

body:
This piece of code takes lot of memory:

int len = Input.size(0);
at::Tensor outputs = torch::zeros({ len });
for (int SliceStart = 0; SliceStart < len; SliceStart += SliceSize)
{
  std::vector<torch::jit::IValue> InputVec;
  InputVec.push_back(Input.slice(0, SliceStart, SliceEnd));
  output = module.forward(InputVec).toTensor();
  for (int i = SliceStart; i < SliceEnd; ++i)
    outputs[i] = output[i - SliceStart];
}

This doesn't (note item() in the last assignment)

int len = Input.size(0);
at::Tensor outputs = torch::zeros({ len });
for (int SliceStart = 0; SliceStart < len; SliceStart += SliceSize)
{
  std::vector<torch::jit::IValue> InputVec;
  InputVec.push_back(Input.slice(0, SliceStart, SliceEnd));
  output = module.forward(InputVec).toTensor();
  for (int i = SliceStart; i < SliceEnd; ++i)
    outputs[i] = output[i - SliceStart].item();
}

Why is it so?


title: How to implying bagging method for LSTM neural network using pyTorch?

body:
Like the title, my question is how to apply the Bagging method for LSTM using the PyTorch library? I have built one using TensorFlow on python. But now to implied into the system using C and C++, the requirement is I need to using PyTorch?

Is there any recommendation for not need to use the PyTorch and applying directly the model built on tensorflow into real predicting in the system??

Please help!