Tag Archives: Deep learning

[Solved] AttributeError: module ‘setuptools._distutils‘ has no attribute ‘version‘

AttributeError: module ‘setuptools._distutils’ has no attribute ‘version’
pytorch tensorboard error: AttributeError: module ‘setuptools._distutils’ has no attribute ‘version’

from torch.utils.tensorboard import SummaryWriter

writer=SummaryWriter("logs")

# writer.add_image()  
#y=x
for i in range(100):

    writer.add_scalar("y=x",i,i)  

writer.close()

Cause of problem:

Setuptools version too high

Solution:

Install lower version setuptools
Enter:
PIP uninstall setuptools
PIP install setuptools = = 59.5.0// it needs to be lower than your previous version

[Solved] Python Keras Error: AttributeError: ‘Sequential‘ object has no attribute ‘predict_classes‘

This article is about using Keras in Python to execute yhat_classes = model.predict_classes(X_test) code with an error: AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’ solution.

model = Sequential()
model.add(Dense(24, input_dim=13, activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(6, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
-
history = model.fit(X_train, y_train, batch_size = 256, epochs = 10, verbose = 2, validation_split = 0.2)
-
score, acc = model.evaluate(X_test, y_test,verbose=2, batch_size= 256)
print('test accuracy:', acc)
-
yhat_classes = model.predict_classes(X_test)

Cause of problem:

This predict was removed in tensorflow version 2.6_ Classes function.

Reference documents: https://keras.rstudio.com/reference/predict_proba.html#details

The following codes can be used:

predict_x=model.predict(X_test) 
classes_x=np.argmax(predict_x,axis=1)

Or

You can also try tensorflow 2.5 or other versions to solve this problem.

Using tensorflow version 2.5, there may be the following warning messages:

tensorflow\python\keras\engine\sequential.py:455: UserWarning: model.predict_classes() is deprecated and will be removed after 2021-01-01. Please use instead:* np.argmax(model.predict(x), axis=-1), if your model does multi-class classification (e.g. if it uses a softmax last-layer activation).* (model.predict(x) > 0.5).astype("int32"), if your model does binary classification (e.g. if it uses a sigmoid last-layer activation).

[Solved] ERROR: No matching distribution found for torch-cluster==x.x.x

Refer to the configuration of others and configure py36 in CONDA virtual environment

conda create -n py36 python=3.6

The default is Python 3 6.0. Later, pytorch = 1.8.0 and cudatoolkit = 11.1.1 are installed successfully, and then pip is used to install
– torch cluster = = 1.5.9
– torch scatter = = 2.0.6
– torch spark = = 0.6.9
– torch spline conv = = 1.2.1

ERROR: No matching distribution found for torch-cluster==1.5.9

After trying various methods on the Internet, it still doesn’t work. Even if you remove the version limit, you still report an error
later, I checked the environment configuration of others I referred to. It was the wrong version of Python I used. I should use Python 3 6.10
then execute in this virtual environment:

conda install python=3.6.10=hcf32534_1

Then execute it

pip install torch-xxxx==x.x.x

You can install it successfully

[Solved] mmdetection benchmark.py Error: RuntimeError: Distributed package doesn‘t have NCCL built in

Cause:
use mmdetection’s tools/benchmark An error occurs when py calculates FPS
the error contents are as follows:

Traceback (most recent call last):
  File "tools/analysis_tools/benchmark.py", line 191, in <module>
    main()
  File "tools/analysis_tools/benchmark.py", line 183, in main
    init_dist(args.launcher, **cfg.dist_params)
  File "D:\Anaconda\envs\eagermot\lib\site-packages\mmcv\runner\dist_utils.py", line 18, in init_dist
    _init_dist_pytorch(backend, **kwargs)
  File "D:\Anaconda\envs\eagermot\lib\site-packages\mmcv\runner\dist_utils.py", line 32, in _init_dist_pytorch
    dist.init_process_group(backend=backend, **kwargs)
  File "D:\Anaconda\envs\eagermot\lib\site-packages\torch\distributed\distributed_c10d.py", line 510, in init_process_group
    timeout=timeout))
  File "D:\Anaconda\envs\eagermot\lib\site-packages\torch\distributed\distributed_c10d.py", line 597, in _new_process_group_helper
    raise RuntimeError("Distributed package doesn't have NCCL "
RuntimeError: Distributed package doesn't have NCCL built in

Cause analysis:
Windows does not support nccl backend

Solution:
1. Locate the following code location

File "D:\Anaconda\envs\eagermot\lib\site-packages\mmcv\runner\dist_utils.py", line 32, in _init_dist_pytorch

2. Add code before 1 (line 32)

backend='gloo'

[Solved] Pyg load dataset Error: attributeerror [pytorch geometry]

AttributeError: ‘GlobalStorage’ object has no attribute ‘train_mask’ Solution

 def create_masks(data):
    """
    Splits data into training, validation, and test splits in a stratified manner if
    it is not already splitted. Each split is associated with a mask vector, which
    specifies the indices for that split. The data will be modified in-place
    :param data: Data object
    :return: The modified data
    """
    if not hasattr(data, "val_mask"):

        data.train_mask = data.dev_mask = data.test_mask = None

        for i in range(20):
            labels = data.y.numpy()
            dev_size = int(labels.shape[0] * 0.1)
            test_size = int(labels.shape[0] * 0.8)

            perm = np.random.permutation(labels.shape[0])
            test_index = perm[:test_size]
            dev_index = perm[test_size:test_size + dev_size]

            data_index = np.arange(labels.shape[0])
            test_mask = torch.tensor(np.in1d(data_index, test_index), dtype=torch.bool)
            dev_mask = torch.tensor(np.in1d(data_index, dev_index), dtype=torch.bool)
            train_mask = ~(dev_mask + test_mask)

            test_mask = test_mask.reshape(1, -1)
            dev_mask = dev_mask.reshape(1, -1)
            train_mask = train_mask.reshape(1, -1)


            if data.train_mask is None:
                data.train_mask = train_mask
                data.val_mask = dev_mask
                data.test_mask = test_mask
            else:

                data.train_mask = torch.cat((data.train_mask, train_mask), dim=0)
                data.val_mask = torch.cat((data.val_mask, dev_mask), dim=0)
                data.test_mask = torch.cat((data.test_mask, test_mask), dim=0)

    else:  # in the case of WikiCS
        data.train_mask = data.train_mask.T
        data.val_mask = data.val_mask.T

    return data

AttributeError: 'GlobalStorage' object has no attribute 'train_mask'
Line 33: Change
if data.train_mask is None: to if 'train_mask' not in data:

[Solved] Error(s) in loading state_dict for GeneratorResNet

**

Error (s) in loading state_dict for GeneratorResNet

**
cause of the problem: check whether we use dataparallel for multi GPU during training. The model generated by this method will automatically add key: module
observe the error message:
you can find that the key values in the model are more modules

Solution:
1. Delete the module

    gentmps=torch.load("./saved_models/generator_%d.pth" % opt.epoch)
    distmps = torch.load("./saved_models/discriminator_%d.pth" % opt.epoch)
    from collections import OrderedDict
    new_gens = OrderedDict()
    new_diss = OrderedDict()
    for k, v in gentmps.items():
        name = k.replace('module.','') # remove 'module.'
        new_gens[name] = v #The value corresponding to the key value of the new dictionary is a one-to-one value.
    for k, v in distmps.items():
        name = k.replace('module.','') # remove 'module.'
        new_diss[name] = v #The value corresponding to the key value of the new dictionary is a one-to-one value.
    generator.load_state_dict(new_gens)
    discriminator.load_state_dict(new_diss)

[Solved] Failed environment install leads to “unable to create process using“ error

Failed environment install leads to “unable to create process using” error

Maybe it’s because there are too many environments installed in CONDA, and you copy them yourself. Suddenly, CONDA fails.

As long as you enter CONDA activate, it will report

Fatal error in launcher: Unable to create process using '"d:\project\gae_gan\ddgk\venv\scripts\python.exe" "D:\Programs\Anaconda3\Scripts\conda.exe" '

Error.

Finally found a very simple solution

Enter directly in the console

***\Anaconda3\Scripts\activate

Then restart console to be fine!
Referenec:
https://stackoverflow.com/questions/59721699/anaconda-is-unable-to-create-process-on-windows10

[Solved] bushi RuntimeError: version_ <= kMaxSupportedFileFormatVersion INTERNAL ASSERT FAILED at /pytorch/caffe2/s

Error Messages:

RuntimeError: version_ <= kMaxSupportedFileFormatVersion INTERNAL ASSERT FAILED at /pytorch/caffe2/serialize/inline_container.cc:132, please report a bug to PyTorch. Attempted to read a PyTorch file with version 3, but the maximum supported version for reading is 2. Your PyTorch installation may be too old. (init at /pytorch/caffe2/serialize/inline_container.cc:132)
frame #0: c10::Error::Error(c10::SourceLocation, std::string const&) + 0x33 (0x7fdcd0189193 in /home/a430/intel/intelpython3/envs/zayn/lib/python3.6/site-packages/torch/lib/libc10.so)
frame #1: caffe2::serialize::PyTorchStreamReader::init() + 0x1f5b (0x7fdcd33119eb in /home/a430/intel/intelpython3/envs/zayn/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #2: caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader(std::string const&) + 0x64 (0x7fdcd3312c04 in /home/a430/intel/intelpython3/envs/zayn/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #3: <unknown function> + 0x6c53a6 (0x7fdd1b2423a6 in /home/a430/intel/intelpython3/envs/zayn/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
frame #4: <unknown function> + 0x2961c4 (0x7fdd1ae131c4 in /home/a430/intel/intelpython3/envs/zayn/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
<omitting python frames>

Some people say there is a problem with the torch version. My torch version is 1.4.0, try upgrading to 1.6.0, torchvision was upgraded to 0.7.0, this problem will be reported:

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

Just follow the hint and add map_location=torch.device(‘cpu’) when loading the model, as follows:
best_model = torch.load("../weights/September01-Unet-se_resnext50_32x4d/checkpoint.pth.tar", map_location=torch.device('cpu'))

[Solved] Pytorch error: RuntimeError: one of the variables needed for gradient computation

Error:
runtimeerror: one of the variables needed for gradient computation has been modified by an inplace operation

Analysis: the new version of pytorch integrates variable and tensor into one tensor, and the inplace operation can be used for variable before, but errors will occur when using tensor

Check whether there is the following expression

x += res #error

Change to

x  = x + res

No error is reported

[Solved] Paramiko error: AttributeError: ‘NoneType’ object has no attribute ‘time’

Error message:


Exception ignored in: <function BufferedFile.__del__ at 0x1104b7d30>
Traceback (most recent call last):
  File "/Users/jerrylin/.conda/envs/pythonProject/lib/python3.8/site-packages/paramiko/file.py", line 66, in __del__
  File "/Users/jerrylin/.conda/envs/pythonProject/lib/python3.8/site-packages/paramiko/channel.py", line 1392, in close
  File "/Users/jerrylin/.conda/envs/pythonProject/lib/python3.8/site-packages/paramiko/channel.py", line 991, in shutdown_write
  File "/Users/jerrylin/.conda/envs/pythonProject/lib/python3.8/site-packages/paramiko/channel.py", line 967, in shutdown
  File "/Users/jerrylin/.conda/envs/pythonProject/lib/python3.8/site-packages/paramiko/transport.py", line 1846, in _send_user_message
AttributeError: 'NoneType' object has no attribute 'time'

Program code:

import paramiko
import sys
import time
# Instantiate SSHClient
client = paramiko.SSHClient()

# Auto add policy to save server's host name and key information, if not added, then hosts no longer recorded in local know_hosts file will not be able to connect
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connect to the SSH server and authenticate with username and password
client.connect()

# Open a Channel and execute commands
stdin, stdout, stderr = client.exec_command('ls data/data_target') # stdout is the correct output, stderr is the error output, and there is one variable with a value

# Print the execution result
print(stdout.read().decode('utf-8'))

# Close SSHClient

client.close()

Solution:

The reason for the error is that the output print conflicts with the close executed by the program. You only need to add a time before the close Sleep (1) is OK

pytorch model.load_state_dict Error [How to Solve]

When pytorch loads the model, if some judgment is used in the model, the judgment is used as the selection execution condition, but it is also saved in the model. However, when calling, the network in the judgment condition is not selected and load_state_Dict is used will report an error. Some operators cannot find the name. For example:

if backbone == "mobilenet":
    self.backbone = mobilenet()
    flat_shape = 1024
    elif backbone == "inception_resnetv1":
    self.backbone = inception_resnet()
else:
    raise ValueError('Unsupported backbone - `{}`, Use mobilenet, inception_resnetv1.'.format(backbone))
    self.avg = nn.AdaptiveAvgPool2d((1,1))
    self.Bottleneck = nn.Linear(flat_shape, embedding_size,bias=False)
    self.last_bn = nn.BatchNorm1d(embedding_size, eps=0.001, momentum=0.1, affine=True)
    if mode == "train": # Judgment condition, test without loading full connection
        self.classifier = nn.Linear(embedding_size, num_classes)

The strict = false option can be added to avoid operators not called in the network:

model2.load_state_dict(state_dict2, strict=False)

[Solved] apex Xavier install torchvision error: illegal instruction

Description: I installed torch vision 0 eight

git clone -b v0.8.1 https://github.com/pytorch/vision.git vision-0.8.1

Installation:

cd vision-0.8.1
sudo /home/nvidia/mambaforge/envs/ultra-fast-lane/bin/python3.6 setup.py install

Note: be sure to make the path and sudo

Error: illegal instruction

Solution: reduce the numpy version. I use numpy = 1.19.3. Success