2019/03/24 - 使用Accord.NET的SVM进行归类

楔子

贵金属纳米粒子独特的局域表面等离子体共振(LSPR)特性使其呈现丰富的颜色,并且其颜色会随着纳米粒子的形貌、周围电介质的变化而千变万化,因此,利用贵金属纳米粒子的颜色变化,实现对其他物质的检测成为可能。Ag纳米粒子和$Hg^{2+}$发生共价置换反应,会导致其颜色的发生变化,从而可以根据Ag纳米粒子颜色的变化,实现对$Hg^{2+}$的痕量检测。那么如何根据Ag纳米粒子既有的颜色变化,实现对$Hg^{2+}$浓度的预测是一个比较好玩的事情。使用神经网络进行预测是其中比较比较方便的一个解决方案。

Accord.NET

Accord.NET是.NET的神经网络库(参考:https://github.com/accord-net/framework/wiki/Classification),可以非常方便的创建SVM并进行学习。本来我没有意愿搞这个,无奈机缘巧合搞了一天,哎,写程序真是非常劳累的事情。言归正传!

具体实现

  • 首先建立读取图片的函数
class FileTool
{

    public String RootPath = "";
    public List<String> FileNames;


    public FileTool(String rootPath = @"C:\Users\Xing\Documents\SVM\image")
    {
        RootPath = rootPath;
        GetFiles();
    }


    public void GetFiles()
    {
        DirectoryInfo root = new DirectoryInfo(RootPath);
        FileInfo[] files = root.GetFiles();
        FileNames = new List<string>(); 

        foreach (var file in files)
        {
            FileNames.Add(Path.Combine(RootPath, file.Name));
        }
    }


    public int[] GetClassfication()
    {
        int[] classfication = new int[FileNames.Count];
        for(int i=0; i< FileNames.Count; i++)
        {
            String baseName = Path.GetFileName(FileNames[i]);
            classfication[i] = int.Parse("" + baseName[0]) - 1;
        }

        return classfication;
    }
}

  • 使用图片的RGB和HSV特征对$Hg^{2+}$的浓度进行预测,可以进行如下处理
double[][] inputs = new double[fileNames.Count][];
int[] outputs = ft.GetClassfication();

for(int m=0; m<fileNames.Count; m++)
{
    Console.WriteLine("Training {0}", m);
    Bitmap image = (Bitmap)Bitmap.FromFile(fileNames[m]);

    int width = image.Width;
    int height = image.Height;

    double RS = 0, GS = 0, BS = 0;
    double HS = 0, SS = 0, VS = 0;

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            Color pixel = image.GetPixel(i, j);

            RS += pixel.R;
            GS += pixel.G;
            BS += pixel.B;

            HSVColor hSVColor = GetHSV(pixel);

            HS += hSVColor.Hue;
            SS += hSVColor.Saturation;
            VS += hSVColor.Value;
        }
    }

    double Rsi = Convert.ToInt32(RS / (width * height));
    double Gsi = Convert.ToInt32(GS / (width * height));
    double Bsi = Convert.ToInt32(BS / (width * height));
    double Hsi = Convert.ToInt32(HS / (width * height));
    double Ssi = Convert.ToInt32(SS / (width * height));
    double Vsi = Convert.ToInt32(VS / (width * height));

    inputs[m] = new double[] { Rsi, Gsi, Bsi, Hsi, Ssi, Vsi };
    //inputs[m] = new double[] { Hsi, Ssi, Vsi };
}

  • 然后使用SVM进行学习
public static void ChiSquare(double[][] inputs, int[] outputs)
{
    // Create the multi-class learning algorithm as one-vs-one with DTW:
    var teacher = new MulticlassSupportVectorLearning<ChiSquare, double[]>()
    {
        Learner = (p) => new SequentialMinimalOptimization<ChiSquare, double[]>()
        {
            Complexity = 100, // Create a hard SVM
        }
    };

    // Learn a multi-label SVM using the teacher
    var svm = teacher.Learn(inputs, outputs);

    // Get the predictions for the inputs
    int[] predicted = svm.Decide(inputs);

    double error = new ZeroOneLoss(outputs).Loss(predicted);

    //MessageBox.Show(inputs.ToString(CSharpMatrixFormatProvider.InvariantCulture));
    //Console.WriteLine(outputs.ToString(CSharpMatrixFormatProvider.InvariantCulture));
    Console.WriteLine(predicted.ToString(CSharpMatrixFormatProvider.InvariantCulture));
    Console.WriteLine(error.ToString(CSharpMatrixFormatProvider.InvariantCulture));
}

第一次进行玩耍,还有很多的参数不会调试,也许以后也不会玩了。

2019/03/12 - Pandas读取数据

#!/usr/bin/env python
# -*- coding=utf-8 -*-


import matplotlib.pyplot as pl
import pandas as pd


Ag_material = pd.read_csv('Au_evap', sep=r'[\s\t,]+', header=None, skiprows=3, engine='python')
pl.plot(Ag_material[0], Ag_material[1])
pl.show()

diel文件下载:diel.zip

2019/03/11 - gcloud服务器初始化计算环境

安装必备软件

sudo apt-get install zip unzip gcc gfortran -y
sudo apt-get update -y

安装历史命令

wget http://git.oschina.net/x242025/Shell/raw/master/CentOS/history.sh
chmod a+x history.sh
bash history.sh
rm history.sh

安装ddscat

wget https://github.com/rusterx/mddscat/releases/download/v1.0.0/linux.mddscat.v1.0.0.zip
unzip linux.mddscat.v1.0.0.zip
rm linux.mddscat.v1.0.0.zip
chmod a+x linux.mddscat.v1.0.0/*
mkdir dda.project

暴露ddscat的位置

echo 'PATH="$PATH:~/linux.mddscat.v1.0.0"' | sudo tee -a ~/.profile
source ~/.profile
sudo ln -s ~/linux.mddscat.v1.0.0/ddscat /usr/bin/ddscat

安装监控软件

cd ~/dda.project
wget https://gitee.com/rusterx/monitor/repository/archive/master.zip
unzip master.zip
rm master.zip
chmod -R 777 monitor/*

安装python环境

sudo apt install python python-pip -y
pip install bottle

暴露监控位置

echo 'PATH="$PATH:~/dda.project/monitor"' | sudo tee -a ~/.profile
source ~/.profile

设置vim偏好

# 设置vim
echo 'set ts=4' | sudo tee -a /etc/vim/vimrc

删除冗余文件并重启

rm ~/init_ddscat.sh
sudo reboot