WindowsでMXNet-Gluon-Style-Transferを使う(GPUなし)

2018年7月25日動作確認

環境

Windows10 Pro 64bit

Anacondaで仮想環境を作成

conda create -n mxnet python=3.6 anaconda
activate mxnet
  • pipのアップデート
python -m pip install --upgrade pip
  • msgpackのインストール
pip install msgpack

MXNetのインストール

pip install mxnet==1.3.0b20180724

Style-Transferのソースをダウンロード

github.com
こちらからZIPファイルをダウンロードして解凍

学習済みモデルのダウンロード

python models/download_model.py

実行

python main.py eval --content-image images/content/venice-boat.jpg --style-image images/styles/wave.jpg --model models/21styles.params --content-size 1024 --cuda 0

将棋盤を作ってみる

先手、後手とも駒台は9枚まで
表示するだけで駒は動かせない

using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace shogiban
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        PictureBox[] picbox = new PictureBox[99];

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Height = 828;
            this.Width = 997;
            int[] ichi_tate = { 50, 129, 208, 287, 366, 445, 524, 603, 682 };
            int[] ichi_yoko = { 134, 213, 292, 371, 450, 529, 608, 687, 766 };
            int[] mochikoma_ichi = { 682, 603, 524, 445, 366, 287, 208, 129, 50 };
            
            for (int i = 0; i < 81; i++)
            {
                picbox[i] = new PictureBox();
                picbox[i].BorderStyle = BorderStyle.FixedSingle;
                picbox[i].Size = new Size(80, 80);
                picbox[i].SizeMode = PictureBoxSizeMode.CenterImage;
                picbox[i].BackColor = Color.FromArgb(255, 245, 245);
                picbox[i].Location = new Point(ichi_yoko[i % 9], ichi_tate[i / 9]);
                
            }
            for(int i = 81; i < 90; i++)
            {
                picbox[i] = new PictureBox();
                picbox[i].BorderStyle = BorderStyle.None;
                picbox[i].Size = new Size(80, 80);
                picbox[i].SizeMode = PictureBoxSizeMode.CenterImage;
                picbox[i].Location = new Point(36, ichi_tate[i % 9]);
            }
            for (int i = 90; i < 99; i++)
            {
                picbox[i] = new PictureBox();
                picbox[i].BorderStyle = BorderStyle.None;
                picbox[i].Size = new Size(80, 80);
                picbox[i].SizeMode = PictureBoxSizeMode.CenterImage;
                picbox[i].Location = new Point(864, mochikoma_ichi[i % 9]);
            }
            this.Controls.AddRange(picbox);
        }

        private void 開くOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Environment.CurrentDirectory;
            ofd.Filter = "テキストファイル|*.txt";
            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                int sentemochikoma = 90;
                int gotemochikoma = 81;
                for(int i = 0; i < 90; i++)
                {
                    picbox[i].Image = null;
                }
                try
                {
                    using (StreamReader sr = new StreamReader(ofd.FileName, Encoding.GetEncoding("Shift_JIS")))
                    {
                        while (sr.Peek() >= 0)
                        {
                            string gyo = sr.ReadLine();
                            if (Regex.IsMatch(gyo, @"^0\s+"))
                            {
                                Regex reg = new Regex(@"^0\s+(?<yoko>\d)(?<tate>\d)(?<koma>\w+)");
                                Match m = reg.Match(gyo);
                                int int_tate;
                                int int_yoko;
                                int.TryParse(m.Groups["tate"].Value, out int_tate);
                                int.TryParse(m.Groups["yoko"].Value, out int_yoko);
                                int pic_num = (int_tate - 1) * 9 + (9 - int_yoko);
                                if (m.Groups["koma"].Value == "玉")
                                {
                                    picbox[pic_num].Image = Properties.Resources._1;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "飛")
                                {
                                    picbox[pic_num].Image = Properties.Resources._2;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "竜")
                                {
                                    picbox[pic_num].Image = Properties.Resources._3;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "角")
                                {
                                    picbox[pic_num].Image = Properties.Resources._4;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "馬")
                                {
                                    picbox[pic_num].Image = Properties.Resources._5;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "金")
                                {
                                    picbox[pic_num].Image = Properties.Resources._6;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "銀")
                                {
                                    picbox[pic_num].Image = Properties.Resources._7;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "成銀")
                                {
                                    picbox[pic_num].Image = Properties.Resources._8;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "桂")
                                {
                                    picbox[pic_num].Image = Properties.Resources._9;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "成桂")
                                {
                                    picbox[pic_num].Image = Properties.Resources._10;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "香")
                                {
                                    picbox[pic_num].Image = Properties.Resources._11;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "成香")
                                {
                                    picbox[pic_num].Image = Properties.Resources._12;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "歩")
                                {
                                    picbox[pic_num].Image = Properties.Resources._13;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                                if (m.Groups["koma"].Value == "と")
                                {
                                    picbox[pic_num].Image = Properties.Resources._14;
                                    picbox[pic_num].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                }
                            }
                            if (Regex.IsMatch(gyo, @"^2\s+"))
                            {
                                Regex reg = new Regex(@"^2\s+(?<yoko>\d)(?<tate>\d)(?<koma>\w+)");
                                Match m = reg.Match(gyo);
                                int int_tate;
                                int int_yoko;
                                int.TryParse(m.Groups["tate"].Value, out int_tate);
                                int.TryParse(m.Groups["yoko"].Value, out int_yoko);
                                int pic_num = (int_tate - 1) * 9 + (9 - int_yoko);
                                if (m.Groups["koma"].Value == "玉")
                                {
                                    picbox[pic_num].Image = Properties.Resources._1;
                                }
                                if (m.Groups["koma"].Value == "飛")
                                {
                                    picbox[pic_num].Image = Properties.Resources._2;
                                }
                                if (m.Groups["koma"].Value == "竜")
                                {
                                    picbox[pic_num].Image = Properties.Resources._3;
                                }
                                if (m.Groups["koma"].Value == "角")
                                {
                                    picbox[pic_num].Image = Properties.Resources._4;
                                }
                                if (m.Groups["koma"].Value == "馬")
                                {
                                    picbox[pic_num].Image = Properties.Resources._5;
                                }
                                if (m.Groups["koma"].Value == "金")
                                {
                                    picbox[pic_num].Image = Properties.Resources._6;
                                }
                                if (m.Groups["koma"].Value == "銀")
                                {
                                    picbox[pic_num].Image = Properties.Resources._7;
                                }
                                if (m.Groups["koma"].Value == "成銀")
                                {
                                    picbox[pic_num].Image = Properties.Resources._8;
                                }
                                if (m.Groups["koma"].Value == "桂")
                                {
                                    picbox[pic_num].Image = Properties.Resources._9;
                                }
                                if (m.Groups["koma"].Value == "成桂")
                                {
                                    picbox[pic_num].Image = Properties.Resources._10;
                                }
                                if (m.Groups["koma"].Value == "香")
                                {
                                    picbox[pic_num].Image = Properties.Resources._11;
                                }
                                if (m.Groups["koma"].Value == "成香")
                                {
                                    picbox[pic_num].Image = Properties.Resources._12;
                                }
                                if (m.Groups["koma"].Value == "歩")
                                {
                                    picbox[pic_num].Image = Properties.Resources._13;
                                }
                                if (m.Groups["koma"].Value == "と")
                                {
                                    picbox[pic_num].Image = Properties.Resources._14;
                                }
                            }
                            if (Regex.IsMatch(gyo, @"^1\s+"))
                            {
                                Regex reg = new Regex(@"^1\s+(?<koma>\w+)");
                                Match m = reg.Match(gyo);
                                if (m.Groups["koma"].Value == "飛" & gotemochikoma < 90)
                                {
                                    picbox[gotemochikoma].Image = Properties.Resources._2;
                                    picbox[gotemochikoma].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    gotemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "角" & gotemochikoma < 90)
                                {
                                    picbox[gotemochikoma].Image = Properties.Resources._4;
                                    picbox[gotemochikoma].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    gotemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "金" & gotemochikoma < 90)
                                {
                                    picbox[gotemochikoma].Image = Properties.Resources._6;
                                    picbox[gotemochikoma].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    gotemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "銀" & gotemochikoma < 90)
                                {
                                    picbox[gotemochikoma].Image = Properties.Resources._7;
                                    picbox[gotemochikoma].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    gotemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "桂" & gotemochikoma < 90)
                                {
                                    picbox[gotemochikoma].Image = Properties.Resources._9;
                                    picbox[gotemochikoma].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    gotemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "香" & gotemochikoma < 90)
                                {
                                    picbox[gotemochikoma].Image = Properties.Resources._11;
                                    picbox[gotemochikoma].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    gotemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "歩" & gotemochikoma < 90)
                                {
                                    picbox[gotemochikoma].Image = Properties.Resources._13;
                                    picbox[gotemochikoma].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    gotemochikoma++;
                                }
                            }
                            if (Regex.IsMatch(gyo, @"^3\s+"))
                            {
                                Regex reg = new Regex(@"^3\s+(?<koma>\w+)");
                                Match m = reg.Match(gyo);
                                if (m.Groups["koma"].Value == "飛" & sentemochikoma < 99)
                                {
                                    picbox[sentemochikoma].Image = Properties.Resources._2;
                                    sentemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "角" & sentemochikoma < 99)
                                {
                                    picbox[sentemochikoma].Image = Properties.Resources._4;
                                    sentemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "金" & sentemochikoma < 99)
                                {
                                    picbox[sentemochikoma].Image = Properties.Resources._6;
                                    sentemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "銀" & sentemochikoma < 99)
                                {
                                    picbox[sentemochikoma].Image = Properties.Resources._7;
                                    sentemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "桂" & sentemochikoma < 99)
                                {
                                    picbox[sentemochikoma].Image = Properties.Resources._9;
                                    sentemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "香" & sentemochikoma < 99)
                                {
                                    picbox[sentemochikoma].Image = Properties.Resources._11;
                                    sentemochikoma++;
                                }
                                if (m.Groups["koma"].Value == "歩" & sentemochikoma < 99)
                                {
                                    picbox[sentemochikoma].Image = Properties.Resources._13;
                                    sentemochikoma++;
                                }
                            }
                        }
                    }
                }
                catch (Exception f)
                {
                    MessageBox.Show(f.Message);
                }
            }
        }

        private void 終了XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 新規作成NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Notepad");
        }

        private void ヘルプHToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string msg = "0: 後手盤上の駒\r\n1: 後手持ち駒\r\n2: 先手盤上の駒\r\n3: 先手持ち駒";
            MessageBox.Show(msg);
        }
    }
}

文字列から特定の行を削除する

  • 数字から始まる行のみを抽出
            if (Clipboard.ContainsText())
            {
                string[] _kugiri = { "\r\n" };
                List<string> lines = new List<string>();
                lines = Clipboard.GetText().Split(_kugiri, StringSplitOptions.RemoveEmptyEntries).ToList();
                lines.RemoveAll(s => !Regex.IsMatch(s, @"^\d+"));
                if (lines.Count() != 0)
                {
                    string result = String.Join("\r\n", lines.ToArray());
                    Clipboard.SetText(result);
                }
            }
  • 数字から始まる行のみを抽出し、さらに先頭の数字を消去する
if (Clipboard.ContainsText())
            {
                string[] _kugiri = { "\r\n" };
                List<string> lines = new List<string>();
                lines = Clipboard.GetText().Split(_kugiri, StringSplitOptions.RemoveEmptyEntries).ToList();
                Regex rgx = new Regex(@"^\d+\s+");
                IEnumerable<string> result2 = 
                    lines.Where(s => Regex.IsMatch(s, @"^\d+"))
                    .Select(s =>
                    {
                        s = rgx.Replace(s, "");
                        return s;
                    });
                if (result2.Count() != 0)
                {
                    string result = String.Join("\r\n", result2.ToArray());
                    Clipboard.SetText(result);
                }
            }

VBAで折れ線グラフを描く

touch-sp.hatenablog.com
上記記事よりさらに細かい設定を加えました。

'グラフを描く
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim graphdata As Range
     
    Set graphdata = Range("A1").CurrentRegion
    
    ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
    ActiveChart.SetSourceData Source:=graphdata
    ActiveChart.Location Where:=xlLocationAsNewSheet
    
    
    With ActiveChart
        '凡例を表示
        .SetElement (msoElementLegendBottom)
        '凡例のプロパティ
        .Legend.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 0)
        .Legend.Format.TextFrame2.TextRange.Font.Size = 28
        '空白セルを線で結ぶ
        .DisplayBlanksAs = xlInterpolated
        'タイトルの書式設定
        .ChartTitle.Text = "タイトル"
        .ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 0)
        .ChartTitle.Format.TextFrame2.TextRange.Font.Size = 32
        .ChartArea.Format.Line.Visible = msoFalse
    End With
    
    
    ActiveChart.FullSeriesCollection(3).Select
    
    With Selection
        .MarkerStyle = 8
        .MarkerSize = 8
        .MarkerBackgroundColor = RGB(0, 0, 0)
        .MarkerForegroundColor = RGB(0, 0, 0)
    End With
    
    With Selection.Format.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 0, 0)
        .Weight = 3#
        .DashStyle = msoLineSolid
    End With
    
    
    ActiveChart.FullSeriesCollection(1).Select
    
    Selection.MarkerStyle = -4142

    With Selection.Format.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Weight = 2.5
        .DashStyle = msoLineSysDash
    End With

    
    ActiveChart.FullSeriesCollection(2).Select
    
    Selection.MarkerStyle = -4142
    
    With Selection.Format.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 0, 255)
        .Weight = 2.5
        .DashStyle = msoLineSysDot
    End With
    
    '縦軸の調整
    ActiveChart.Axes(xlValue).MajorUnit = 1
    ActiveChart.Axes(xlValue).TickLabels.Font.Size = 28
    ActiveChart.Axes(xlValue).TickLabels.Font.Color = RGB(0, 0, 0)
    
    '横軸の調整
    ActiveChart.Axes(xlCategory).TickLabels.Font.Size = 28
    ActiveChart.Axes(xlCategory).TickLabels.Font.Color = RGB(0, 0, 0)
    ActiveChart.Axes(xlCategory).TickLabels.NumberFormatLocal = "yy""年""m""月"";@"
    ActiveChart.Axes(xlCategory).MajorUnit = 1
    ActiveChart.Axes(xlCategory).MajorUnitScale = xlMonths
    ActiveChart.Axes(xlCategory).Format.Line.ForeColor.RGB = RGB(0, 0, 0)
    
    'プロットエリア塗りつぶし
    ActiveChart.PlotArea.Format.Fill.ForeColor.RGB = RGB(255, 255, 204)
    ActiveChart.PlotArea.Format.Line.ForeColor.RGB = RGB(0, 0, 0)
    

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Rできれいな図を描く(棒グラフ) Part 2

touch-sp.hatenablog.com

mydata <- c(1.6,1.2,1.23,1.08,1.01,1.00,1.12,1.06,1.15,1.28,1.35)

mycol <- rep(rgb(90,155,215, maxColorValue=255),11)
mycol[6] <- "red"

png("myGraph.png", width=900, height=600, bg="transparent")

#外枠を描く
plot(0,0,type="n", ylim=c(0,2.2), xlim=c(0,1), ann=F, yaxs="i", xaxs="i", axes=F)
rect(0,0,1,2.2, col = "white")
rect(0.283,0,0.717,2.2, col=rgb(253,230,200, maxColorValue=255), border=NA)

#軸を描く
abline(h=0.5, col="black", lty="dashed")
abline(h=1.5, col="black", lty="dashed")
abline(h=2, col="black", lty="dashed")
abline(h=1, col="red", lwd=2,lty="solid")
abline(h=2.2, col="black", lty="solid")

#棒グラフを描く
par(new=T)
barplot(mydata, col=mycol, ylim=c(0,2.2), yaxt="n", space=0.5)

dev.off()

f:id:touch-sp:20180425115826p:plain:w500

Excel VBAだと横軸の一本だけ色やスタイルを変えることはできない。
また折れ線グラフと棒グラフを一緒に描くと必ず折れ線グラフが上にくる。