Giter Club home page Giter Club logo

cnn-ga's Introduction

CNN-GA

Official implementation of the paper Automatically Designing CNN Architectures Using the Genetic Algorithm for Image Classification.

This code has been updated for the adaption to torchvision 0.12 and pytorch 1.11 [2022/02/25]

Dependencies

  • numpy
  • torchvision 0.12
  • pytorch 1.11

Installation

  1. Clone this repo by running git clone https://github.com/yn-sun/cnn-ga.
  2. Alter hyperparameters in global.ini.
  3. Run python evolve.py.

Citing CNN-GA

It would be greatly appreciated if the following paper can be cited when the code has helped your research.

@article{sun2020automatically,
  title={Automatically designing CNN architectures using the genetic algorithm for image classification},
  author={Sun, Yanan and Xue, Bing and Zhang, Mengjie and Yen, Gary G and Lv, Jiancheng},
  journal={IEEE transactions on cybernetics},
  volume={50},
  number={9},
  pages={3840--3854},
  year={2020},
  publisher={IEEE}
}

cnn-ga's People

Contributors

benchenas avatar mrzilinxiao avatar yn-sun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cnn-ga's Issues

FileNotFoundError: [Errno 2] No such file or directory: './populations/after_00.txt'

2020-08-09 23:58:27,971 INFO : *************************
2020-08-09 23:58:27,971 INFO : Initialize from existing population data
2020-08-09 23:58:27,971 INFO : Initialize from 0-th generation
Traceback (most recent call last):
File "evolve.py", line 114, in
evoCNN.do_work(max_gen=20)
File "evolve.py", line 84, in do_work
pops = Utils.load_population('begin', gen_no)
File "/home/guohy/cnnga/utils.py", line 365, in load_population
f = open(after_file_path)
FileNotFoundError: [Errno 2] No such file or directory: './populations/after_00.txt'

Could you tell me how to solve the problem above?Thank you very much.

ValueError: attempt to get argmin of an empty sequence

2020-08-25 10:54:39,366 INFO : *************************
2020-08-25 10:54:39,367 INFO : Initialize...
2020-08-25 10:54:39,375 INFO : EVOLVE[0-gen]-Begin to evaluate the fitness
2020-08-25 10:54:39,375 INFO : Begin to generate python files
2020-08-25 10:54:39,411 INFO : Finish the generation of python files
2020-08-25 10:54:39,412 INFO : Query fitness from cache
2020-08-25 10:54:39,413 INFO : Total hit 0 individuals for fitness
2020-08-25 10:55:39,475 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-25 10:55:39,476 INFO : Begin to train indi0000
Files already downloaded and verified
Files already downloaded and verified
2020-08-25 10:56:40,380 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-25 10:56:40,381 INFO : Begin to train indi0001
Files already downloaded and verified
Files already downloaded and verified
2020-08-25 10:57:40,516 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-25 10:57:40,517 INFO : Begin to train indi0002
Files already downloaded and verified
Files already downloaded and verified
2020-08-25 11:02:40,716 INFO : GPU_QUERY-None of the GPU is occupied
2020-08-25 11:02:40,720 INFO : Add record into cache, id:831bfb3f88a125297edbf9deb98e30ffc8be4b20c140e4c29d70497d, acc:0.00000
2020-08-25 11:02:40,724 INFO : Add record into cache, id:7ec9dc0cce6d24d94890ed2069027ccd7ad61e536d8909b216d2c805, acc:0.00000
2020-08-25 11:02:40,729 INFO : Add record into cache, id:e9c128d79771d9ae8b6a4822a2d6bdc00cf0fcae6d3d1bb8ff99bc60, acc:0.00000
2020-08-25 11:02:40,740 INFO : EVOLVE[0-gen]-Finish the evaluation
2020-08-25 11:02:40,745 INFO : EVOLVE[1-gen]-Begin to crossover and mutation
2020-08-25 11:02:40,748 WARNING : The 2-th try to find the position for do crossover
2020-08-25 11:02:40,753 WARNING : The 3-th try to find the position for do crossover
2020-08-25 11:02:40,753 WARNING : The 4-th try to find the position for do crossover
2020-08-25 11:02:40,753 INFO : Position 1 for indi0002, positions 1 for indi0000
2020-08-25 11:02:40,754 INFO : Change the input channel of unit at 1 to 64 that is the output channel of unit at 0 in indi0002
2020-08-25 11:02:40,754 INFO : Change the input channel of unit at 1 to 64 that is the output channel of unit at 0 in indi0000
2020-08-25 11:02:40,754 INFO : CROSSOVER-2 offspring are generated, new:2, others:0
2020-08-25 11:02:40,768 INFO : MUTATION-mutated individuals:0[ADD: 0,REMOVE: 0,CHANNEL: 0,POOL: 0, no_changes:2
2020-08-25 11:02:40,777 INFO : EVOLVE[1-gen]-Finish crossover and mutation
2020-08-25 11:02:40,778 INFO : EVOLVE[1-gen]-Begin to evaluate the fitness
2020-08-25 11:02:40,779 INFO : Begin to generate python files
2020-08-25 11:02:40,797 INFO : Finish the generation of python files
2020-08-25 11:02:40,803 INFO : Query fitness from cache
2020-08-25 11:02:40,809 INFO : Hit the cache for indi0000, key:831bfb3f88a125297edbf9deb98e30ffc8be4b20c140e4c29d70497d, acc:0.00000, assigned_acc:-1.00000
2020-08-25 11:02:40,819 INFO : Hit the cache for indi0001, key:e9c128d79771d9ae8b6a4822a2d6bdc00cf0fcae6d3d1bb8ff99bc60, acc:0.00000, assigned_acc:-1.00000
2020-08-25 11:02:40,819 INFO : Total hit 2 individuals for fitness
2020-08-25 11:02:40,819 INFO : indi0000 has inherited the fitness as 0.00000, no need to evaluate
2020-08-25 11:02:40,822 INFO : indi0001 has inherited the fitness as 0.00000, no need to evaluate
2020-08-25 11:02:40,825 INFO : None offspring has been evaluated
2020-08-25 11:02:40,828 INFO : EVOLVE[1-gen]-Finish the evaluation
Traceback (most recent call last):
File "evolve.py", line 114, in
evoCNN.do_work(max_gen=20)
File "evolve.py", line 107, in do_work
self.environment_selection()
File "evolve.py", line 59, in environment_selection
min_idx = np.argmin(first_selectd_v_list)
File "<array_function internals>", line 6, in argmin
File "/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py", line 1267, in argmin
return _wrapfunc(a, 'argmin', axis=axis, out=out)
File "/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py", line 58, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File "/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py", line 47, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
ValueError: attempt to get argmin of an empty sequence

I've also 20gb of RAM left when i got this error. Also, the fitness values are 0 at all times. Is there any way to fix this?

log file not getting created. Not able to understand why this error is occuring.

2020-08-06 20:24:52,755 INFO : *************************
2020-08-06 20:24:52,755 INFO : Initialize...
2020-08-06 20:24:52,765 INFO : EVOLVE[0-gen]-Begin to evaluate the fitness
2020-08-06 20:24:52,765 INFO : Begin to generate python files
2020-08-06 20:24:52,815 INFO : Finish the generation of python files
2020-08-06 20:24:52,815 INFO : Query fitness from cache
2020-08-06 20:24:52,815 INFO : Total hit 0 individuals for fitness
2020-08-06 20:25:52,926 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-06 20:25:52,926 INFO : Begin to train indi0000
Files already downloaded and verified
Files already downloaded and verified
Exception occurs, file:indi0000, pid:1424...[Errno 2] No such file or directory: './log/indi0000.txt'
Process Process-1:
Traceback (most recent call last):
File "C:\Users\cnn-ga\cnn-ga\scripts\indi0000.py", line 164, in do_work
m.log_record('Used GPU#%s, worker name:%s[%d]'%(gpu_id, multiprocessing.current_process().name, os.getpid()), first_time=True)
File "C:\Users\cnn-ga\cnn-ga\scripts\indi0000.py", line 101, in log_record
f = open('./log/%s.txt'%(self.file_id), file_mode)
FileNotFoundError: [Errno 2] No such file or directory: './log/indi0000.txt'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\cnn-ga\cnn-ga\scripts\indi0000.py", line 170, in do_work
m.log_record('Exception occur:%s'%(str(e)))
File "C:\Users\cnn-ga\cnn-ga\scripts\indi0000.py", line 101, in log_record
f = open('./log/%s.txt'%(self.file_id), file_mode)
FileNotFoundError: [Errno 2] No such file or directory: './log/indi0000.txt'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Anaconda\anacondaa\envs\testt\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "C:\Users\Anaconda\anacondaa\envs\testt\lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\cnn-ga\cnn-ga\scripts\indi0000.py", line 172, in do_work
m.log_record('Finished-Acc:%.3f'%best_acc)
File "C:\Users\cnn-ga\cnn-ga\scripts\indi0000.py", line 101, in log_record
f = open('./log/%s.txt'%(self.file_id), file_mode)
FileNotFoundError: [Errno 2] No such file or directory: './log/indi0000.txt'

global.ini

Could you please provide an example of a working configuration file "global.ini" to test the code with it?
I keep getting endless errors of NoSectionError and NoOptionError
Providing a working file to start with it would be greatly helpful.

Thank you so much in advance

GPU_QUERY-No available GPU

hello, i am new to using a gpu, so sorry for the stupid question:
I get this error:

2020-08-14 19:13:26,978 INFO : Total hit 0 individuals for fitness
2020-08-14 19:14:27,022 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-14 19:14:27,022 INFO : Begin to train indi0000

0.0%
0.6%
2.6%
4.7%
6.8%
8.8%
10.9%
11.3%
11.4%
13.0%
15.1%
17.2%
19.2%
21.2%
23.3%
25.1%
27.2%
28.8%
31.0%
33.0%
35.2%
37.3%
39.4%
41.5%
43.3%
45.2%
47.2%
49.1%
51.1%
53.1%
55.1%
57.2%
59.2%
61.1%
63.0%
65.0%
66.9%
68.8%
70.5%
72.4%
74.4%
76.1%
78.1%
80.0%
82.0%
83.9%
85.8%
87.8%
89.8%
91.8%
93.8%
95.9%
97.2%
98.6%
100.0%2020-08-14 19:15:27,398 INFO : GPU_QUERY-No available GPU
2020-08-14 19:20:27,442 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-14 19:20:27,443 INFO : Begin to train indi0001
2020-08-14 19:21:27,494 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-14 19:21:27,495 INFO : Begin to train indi0002
2020-08-14 19:22:27,554 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-14 19:22:27,555 INFO : Begin to train indi0003
2020-08-14 19:23:27,608 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-14 19:23:27,608 INFO : Begin to train indi0004
2020-08-14 19:24:27,659 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-14 19:24:27,659 INFO : Begin to train indi0005
2020-08-14 19:25:27,711 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use
2020-08-14 19:25:27,712 INFO : Begin to train indi0006
2020-08-14 19:26:27,762 INFO : GPU_QUERY-Available GPUs are: [0], choose GPU#0 to use

global.ini:

[settings]
pop_size = 20
gpu_num =
genetic_prob = 0.9, 0.2
mutation_probs = 0.7, 0.1, 0.1, 0.1

[evolution_status]
is_running = 1

[gpu_running_status]
cpu0 =
cpu1 =
cpu2 =
cpu3 =
cpu4 =
cpu5 =

[network]
max_length = 50
conv_limit = 5, 15
pool_limit = 3, 3
input_channel = 3
num_class = 10
epoch = 350
output_channel = 64, 128, 256

  • I have one gpu NVIDIA GeForce RTX 2080 SUPER
  • i have tried setting gpu_num=1 and still get errors
  • not sure how to fix this issue, please advice.

thank you

Ask for help

Can you please tell me the specific versions of numpy, pytorch and torchvision? thank you

local variable 'm' referenced before assignment

Traceback (most recent call last):
File "/home/guohy/.conda/envs/guohy/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
self.run()
File "/home/guohy/.conda/envs/guohy/lib/python3.7/multiprocessing/process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "/home/guohy/cnnga/scripts/indi0019.py", line 178, in do_work
m.log_record('Finished-Acc:%.3f'%best_acc)
UnboundLocalError: local variable 'm' referenced before assignment

Could you tell me how to solve the problem?Thank you very much.

ImportError: cannot import name 'DenseUnit' from 'genetic.population'

utils.py imports DenseUnit from genetic.population, and it doesn't exist there, so I get this error:

File "utils.py", line 5, in
from genetic.population import Population, Individual, DenseUnit, ResUnit, PoolUnit

ImportError: cannot import name 'DenseUnit' from 'genetic.population'

the accurancy keeps 0

I am sorry to disturb you again. After solving the previous problems, the accurancy keeps 0 after running "evolve.py" as follows:
...
Files already downloaded and verified
Files already downloaded and verified
2020-08-19 10:52:52,166 INFO : GPU_QUERY- GPUs [1] are occupying
2020-08-19 10:57:52,349 INFO : GPU_QUERY-None of the GPU is occupied
2020-08-19 10:57:52,351 INFO : Add record into cache, id:1b975a586574bfedd225dbed0f9dc497f9cc55f74572172619e9a638, acc:0.00000
2020-08-19 10:57:52,351 INFO : Add record into cache, id:9259b46a0974ef0071b9ac50d3e69f4ff90ba974af520f315fafa40b, acc:0.00000
2020-08-19 10:57:52,352 INFO : Add record into cache, id:15a9b0562fb76c0e96e86a9d276ae8f788be5460d85ca25145b2b46e, acc:0.00000
2020-08-19 10:57:52,352 INFO : Add record into cache, id:ba08bd49c884d2ffb9b4ee9d5e454ac4fe518256b1a3bc350c341277, acc:0.00000
2020-08-19 10:57:52,352 INFO : Add record into cache, id:48f12b4e99280b1dda33bd44b63cca162f606e9b9c3036508dd77e3a, acc:0.00000
2020-08-19 10:57:52,352 INFO : Add record into cache, id:7ddcec220d7de35728f30771f6c4f332652fd44d68dc40fec349afa6, acc:0.00000
2020-08-19 10:57:52,353 INFO : Add record into cache, id:4fa525b8da73d7880f748c2003de76b3b3c3e7a0e3fa0d7c1dcc938e, acc:0.00000
2020-08-19 10:57:52,353 INFO : Add record into cache, id:e7f85dd5401695ae54ff774c885ab1e2af9996f40d260124df1c64d4, acc:0.00000
2020-08-19 10:57:52,353 INFO : Add record into cache, id:cbe15d4f1b7d9be4e00649d4c101a066b95009cd0a57460f89562f84, acc:0.00000
2020-08-19 10:57:52,353 INFO : Add record into cache, id:80382510c2a2f52d6b593a855abf0232689d58a91fbeb860f5d0af31, acc:0.00000
2020-08-19 10:57:52,354 INFO : Add record into cache, id:5e85678c6f86431656442fa38c1bedc3b22ba1ddaf50dc6b5c5df107, acc:0.00000
2020-08-19 10:57:52,354 INFO : Add record into cache, id:c1433e69c26c51779063461d649a74928386b1c660ffa1d9897e6787, acc:0.00000
2020-08-19 10:57:52,354 INFO : Add record into cache, id:3a4f746073454172339c660be7651b15d84b57b68804b09ec86541a4, acc:0.00000
2020-08-19 10:57:52,354 INFO : Add record into cache, id:9af3ec9820cd1e56c07001167af9a7eefdae8f281c82700444cad206, acc:0.00000
2020-08-19 10:57:52,354 INFO : Add record into cache, id:baf9d016bce544344f47411e1d08b9e5451ef368b3d9916e63afd829, acc:0.00000
2020-08-19 10:57:52,355 INFO : Add record into cache, id:8183eed781881893e6e34d256d079978c5f108284e082407e7581186, acc:0.00000
2020-08-19 10:57:52,355 INFO : Add record into cache, id:61ef1d76d618f967f0f6ae0e8f89a2d17b8154f99057edeed765708a, acc:0.00000
2020-08-19 10:57:52,355 INFO : Add record into cache, id:036f33574d7252ad2b32cae4ba3ac9147f551d068d4159fcc0a278a8, acc:0.00000
2020-08-19 10:57:52,355 INFO : EVOLVE[1-gen]-Finish the evaluation
Traceback (most recent call last):
File "evolve.py", line 114, in
evoCNN.do_work(max_gen=20)
File "evolve.py", line 107, in do_work
self.environment_selection()
File "evolve.py", line 59, in environment_selection
min_idx = np.argmin(first_selectd_v_list)
File "<array_function internals>", line 6, in argmin
File "/home/guohy/.conda/envs/guohy/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 1222, in argmin
return _wrapfunc(a, 'argmin', axis=axis, out=out)
File "/home/guohy/.conda/envs/guohy/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 58, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File "/home/guohy/.conda/envs/guohy/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 47, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
ValueError: attempt to get argmin of an empty sequence

Could you tell me how to solve the problem?Thank you for all your assistance!

Minimum memory requirements to execute this code successfully.

(qurat4) D:\Qurat_Data\cnn-ga-master\cnn-ga-master>python evolve.py
2022-04-27 04:11:29,608 INFO : *************************
2022-04-27 04:11:29,608 INFO : Initialize from existing population data
2022-04-27 04:11:29,609 INFO : Initialize from 0-th generation
2022-04-27 04:11:29,613 INFO : EVOLVE[0-gen]-Begin to evaluate the fitness
2022-04-27 04:11:29,613 INFO : Begin to generate python files
Traceback (most recent call last):
File "evolve.py", line 115, in
evoCNN.do_work(max_gen=20)
File "evolve.py", line 94, in do_work
self.fitness_evaluate()
File "evolve.py", line 23, in fitness_evaluate
fitness.generate_to_python_file()
File "D:\Qurat_Data\cnn-ga-master\cnn-ga-master\genetic\evaluate.py", line 16, in generate_to_python_file
Utils.generate_pytorch_file(indi)
File "D:\Qurat_Data\cnn-ga-master\cnn-ga-master\utils.py", line 454, in generate_pytorch_file
part1, part2, part3 = cls.read_template()
File "D:\Qurat_Data\cnn-ga-master\cnn-ga-master\utils.py", line 401, in read_template
part3.append(line)
MemoryError

My Machine has 64.0 GB of RAM. Nvidia GPU 3060 with 12 GB graphics memory. Processor: AMD Ryzen 5 5600X 6-Core Processor 3.70 GHz

Kindly let me know the minimum graphics memory requirements and other computational resources to execute this code successfully. Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.