[2] 实验编程 · 2021年2月23日

Proj.cnn

cnn环境配置

conda create -n cnn python=3.5

conda install Numpy pillow wxPython matplotlib IPython PyOpenGL
cp -r installed/spectral/ .
python setup.py install

conda install scipy
conda install scikit-image
# conda install scikit-learn -y # No module named 'sklearn'
pip install -U scikit-learn
# conda install tensorflow
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn tensorflow==1.14.0 
conda install six 
conda install pandas
conda install matplotlib

conda install -c https://conda.binstar.org/menpo opencv
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python

cnn使用方法

% 2. 修改 patchsize.py
patchsize.py % modify patchsize 1,3,5
sh temp_mv.sh cnnX % move temporal files to cnnX
% 1. 修改 IndianPinesCNN.py
IndianPinesCNN.py  % settting
% 3. 修改 DataSet_Preparation_G.py,
python DataSet_Preparation_G.py | tee out.txt
% 4. 修改 CNN_G.py
python CNN_G.py | tee -a out.txt
% 5. 修改 Decoder_G.py
python Decoder_G.py | tee -a out.txt
./m_oa.py   % cal OA 
./m_show.py % display results

修改路径:CNN_G.py m_path

  1. patchsize.py 修改
  2. IndianPinesCNN.py 修改
    NUM_CLASSES = 16, 换数据改
    CHANNELS = 220 (波段数),换数据改
    weights = tf.get_variable(‘weights’, shape=[KERNEL_SIZE, KERNEL_SIZE, CHANNEL, conv1_channels], 换数据改 (波段数 CHANNELS) % # Conv 1
    z = tf.nn.conv2d(x_image, weights, strides=[1, 1, 1, 1], padding=’SAME‘), patchsize>1改为VALID
    z = tf.nn.conv2d(h_pool1, weights, strides=[1, 1, 1, 1], padding=’SAME‘), patchsize>1改为VALID
  3. DataSet_Preparation_G.py 修改
    COUNT=200; 进行过采样之后每一个类别内的样本数目, 基本不改
    OUTPUT_CLASSES=16; GT中类别数 (6),换数据改
    TEST_FRAC = 0.25; 测试数据百分比, 基本不改
    input_mat=scipy.io.loadmat(os.path.join(DATA_PATH,’gf1.mat’))[‘gf1’],换数据-文件夹改
    target_mat =scipy.io.loadmat(os.path.join(DATA_PATH,’gf1_gt.mat’))[‘gf1_gt’](要用cv读tif?),换数据-文件夹改
    TRAIN_PATCH = TRAIN_PATCH. reshape((-1,220,PATCH_SIZE,PATCH_SIZE)) ,[line 178:] (波段数)
    注意修改循环次数,GT是通过ENVI ->roi2img ->window save as tif.
  4. 修改 CNN_G.py,或, g_cnn.py … based on (a)
    TRAIN_FILES = 8, [line 38:]
    TEST_FILES = 6, [line 39:]
    saver.save([line 225:] (路径m_path)
    temp_ image = temp_ image. reshape (temp_ image. shape[0], IMAGE_ SIZE,IMAGE_ SIZE, 220 (波段数), [line 125:]
  5. 修改 Decoder\_G.py,或 g_decoder.py
    from sklearn.metrics import accuracy_score, [line 1:]
    input_mat=scipy.io.loadmat(os.path.join(DATA_PATH,’gyl321.mat‘))[‘gyl321‘], [line 24]
    target_mat=scipy.io.loadmat(os.path.join(DATA_PATH,’gyl321gt.mat‘))[‘gyl321gt‘], [line 25]
    注意修改循环类别个数
    model_name = ‘model- spatial- CNN-11X11. ckpt-3999′ (patchsize), [line 28:]

在运行完一个patch_size的值之后,最好文件夹下面所生成的 checkpoint 文件和__pycache__ 文件 删掉,不然会影响不同值的运行.

参考来源1,参考来源2