Tag Archives: Deep learning

Keras-nightly Import package Error: cannot import name ‘Adam‘ from ‘keras.optimizers‘

Version keras nightly = 2.5.0.dev2021032900

Error information

    from keras.optimizers import Adam
ImportError: cannot import name 'Adam' from 'keras.optimizers' 

Solution

error code

from keras.optimizers import Adam
opt = Adam(lr=lr, decay=lr/epochs)

modify

from keras.optimizers import adam_v2
opt = adam_v2.Adam(learning_rate=lr, decay=lr/epochs)

reason

After the keras library is updated, the package cannot be imported in the original way. Open the optimizers.py source code and find the following two key codes. You can see that Adam import has changed, so it is modified as above.

from keras.optimizer_v2 import adam as adam_v2
'adam': adam_v2.Adam,

Keras import a custom metric model error: unknown metric function: Please ensure this object is passed to`custom_object‘

Keras’s model defines metric or loss,
there is no problem when saving to H5, but when using load_ When importing the model, an error will be reported:

unknown metric function: HammingScore. Please ensure this object is passed to the custom_ objects argument.

This is because the custom parameters are not passed in. There are two solutions:

          1. if you only need to predict and no longer train, you can add

        compile = false directly

model = keras.models.load_model('model.h5', compile = False)

If you need further training or modification, add your own metrics code and compile it again

model.compile(loss='binary_crossentropy',
              optimizer=Ada,
              metrics=[HammingScore]) # 这里HammingScore是我自定义的metric
      1. when importing, the user-defined metric/loss is passed into

Custom as a key value_ objects

      1. :
model = keras.models.load_model('model.h5', custom_objects={'HammingScore': HammingScore} )

Note that the key value should be consistent

Successfully solved the problem of “runtimee” in RESNET dataset classification rror:expected scalar type Long but found Float”

Recently, I encountered some mistakes as shown in the title when doing deep learning classification, but I don’t know how to modify them. Finally, after exploration, I successfully solved them
the problems and solutions are reported directly below.

Error

Solution

In practice, the label of classification should be long, and the image should be float32
therefore, modifying the data type will succeed, but it doesn’t matter. I’ll share it with you after I solve it successfully!

Condahttperror:HTTP000 Connection Failed for URL https://repo.anaconda.com/pkgs/main/win-6

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/win-64/repodata.json>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

Solution:
Win + R

then find

and replace it with

channels:
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
show_channel_urls: true
ssl_verify: false

mmdetection Error when running voc.py: KeyError: ‘NumClassCheckHook is already registered in hook‘

This error is usually caused by the version of mmcv
just click in one by one according to the following error prompt to find utils.py file:
change the code in the circle below:

add in brackets:

finally, it can run successfully.


Similar questions:
keyerror: ‘XXX is already registered in xxx’


Results: it runs perfectly

Cuda Runtime error (38) : no CUDA-capable device is detected

When training the model in vscode, the following errors appear:
CUDA runtime error (38): no CUDA capable device is detected at/Torch/aten/SRC/THC/thcgeneral cpp:51

The reason is that CUDA is not selected correctly, so you need to check how many graphics cards you have first:
Enter Python in terminal; Enter the following code

import torch
print(torch.cuda.device_count()) #Number of available GPUs

View CUDA version

nvcc --version # Check your own CUDA version

I have two graphics cards in my library. Choose one from 0 or 1 and write it before
model = torch.nn.dataparallel (net). Cuda()

os.environ['CUDA_VISIBLE_DEVICES'] = '0'
model = torch.nn.DataParallel(net).cuda()

Problem solving.

To solve the problem of increasing video memory when training network (torch)

Method 1

torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True

Principle:

Cundnn follows the following criteria:

    if the dimension or type of network input data changes little, set    torch.backends.cudnn.benchmark  =  true   It can increase the operation efficiency; If the input data of the network changes every iteration, cndnn will find the optimal configuration every time, which will improve the operation efficiency

     
    Method 2

    Tensor calculation should be written as follows:

    train_loss += loss.item()

Viewing events.out.tfevents file visually in tensorboard

Command line

Tensorboard — logdir = event dir (event save path)

 
After meeting the problem of “no dashboards are active for the current data set”, it is not solved after searching on the Internet for several hours; I tried several times according to my own ideas and finally solved the problem. Now I share the solutions as follows:

 
1> Find the path of tensorboard, and then CD to the path;

2> Copy and paste the directory of the file to be displayed to the path of the tensorboard;

3> CD to the directory of tensorboard, enter:

tensorboard –logdir=   
4> Copy address http://localhost : 6006 /, input to browser, finish.

 

 

Record a problem of no module named ‘tensorflow. Examples’ and’ tensorflow. Examples. Tutorials’ in tensorflow 2.0

1: No module named ‘tensorflow. Examples’
I downloaded tensorflow directly from the Internet, which is version 2.5. The path to add examples is in C:// program data/anaconda3/envs/tensorflow/lib/site packages/tensorflow, which is similar to that on the Internet_ In the core folder, there is no such folder in version 2.5, so all the next operations are performed in site package/tensorflow.

First of all, you have to go to the official website of tensorflow( https://github.com/tensorflow/tensorflow/tree/master/tensorflow )Download the examples folder and copy it to the site package/tensorflow folder mentioned above. If you continue to run your code, there will be a problem of no module named ‘tensorflow. Examples. Tutorials’.

2: No module named ‘tensorflow. Examples. Tutorials’
in the site package/tensorflow folder, click the examples file you just copied in (I believe you have downloaded many tutorials files on the Internet, just copy them in directly), and then the code can run

Note: if you have not downloaded to the tutorials file, you can go to the official website of tensorflow, and then adjust the version to the version before 2.40, you will find the tutorials file in the examples folder (this method has not been tested, if it is feasible after the test, you can leave a message in the comments area, thank you).

File C:\Users\admin\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because running scripts

CONDA activate error

File C:\Users\admin\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because running scripts is disabled on this system.

resolvent

Refer to the answer of stackoverflow
the steps are as follows:

    open the PowerShell in administrator mode. If you don’t know how to open it as an administrator, you can press win + R , then enter shell , and then press Ctrl + shift + Enter . typing

    Set-ExecutionPolicy RemoteSigned
    

This should allow your system to run CONDA scripts. If you want to set it back, type it in the PowerShell administrator mode

Set-ExecutionPolicy Restricted

[Solved] RuntimeError: cuda runtime error: device-side assert trigger

In this way, when running fastercnn, we need to change the original model’s 21 categories to our own number of categories. After the first modification, no error will be reported in the run, and after the second modification, an error will be reported as follows:
1 block: [0,0,0], thread: [16,0,0] assertion T & gt= 0 && amp; t < n_ Classes failed.
2 runtime error: CUDA runtime error (59): device side assert triggered
the main solutions on the Internet are as follows:

The reason for this problem is that there are tags in the training data that exceed the number of categories. For example, I set up a total of 8 classes, but if 9 appears in the tag in the training data, this error will be reported. So here’s the problem. There’s a trap. If the tag in the training data contains 0, the above error will also be reported. This is very weird. Generally, we start counting from 0, but in Python, the category labels below 0 have to report an error. So if the category label starts from 0, add 1 to all category labels.

Solution:
The first time I ran the program, I found that there were 16 categories (I deleted 4 categories, but I didn’t find them). After running the program, I found that there were four more categories, so I deleted these four categories. However, when I ran the program again, I reported the above error. The reason is that every time we
run the program, we have to delete the cache generated by the last run, because I didn’t delete it, so the program thought it was 16 categories, But only 12 categories are provided. So if you report this error, you can delete the cache and run it again