Fprime学习(一)-----安装fprime

前言

其实应该有个序章对fprime这个玩意进行介绍,但是我现在还不知道fprime到底怎么部署,能干啥,所以先从安装和tutorials开始写,记录下一些坑

安装过程

官方教程

官方教程是使用python的venv进行虚拟环境管理的,具体按照官方教程操作应该就可以了(虽然我没有试过),官方安装教程在docs/install.md下。我自己安装的环境比较特殊,因此记录下几个坑。
安装的依赖如下:

- Linux or Mac OS X operating system (or Windows Subsystem for Linux on Windows)
- CMake 3.5 or newer https://cmake.org/download/. CLI tool must be available on system path.
- Bash or Bash compatible shell
- CLang or GCC compiler
- Python 3.5+ and PIP https://www.python.org/downloads/
- Python Virtual Environment * (pip install venv or pip install virtualenv) 

如果想要在系统路径下进行安装的话,需要确保系统的/usr/bin/python的软链接是指向python3.x的。

安装过程

我自己的安装环境如下,比较特殊,主要还是conda和ros的问题

- kubuntu 18.04(x64)
- cmake 3.10
- ros1 melodic 和 ros2
- anaconda(用于配置虚拟环境)

conda和ros冲突问题

conda和ros会出现冲突,我个人遇到的问题有以下几个:

  1. 如果在bashrc中有conda环境变量的初始化,那么你初始的python是指向conda的base的
  2. 在conda中调用opencv,会指向ros的opencv

使用conda创建虚拟环境并安装

安装conda的操作就略过了,首先创建一个虚拟环境

conda create -n fprime python=3.7
conda activate fprime

然后对必要包进行升级

pip install --upgrade wheel setuptools pip 

然后就直接pip安装fprime包即可,我这里和官方的稍有不同

cd fprime
cd  Fw/Python
python setup.py install
cd ../Gds
python setup.py install

先转到你clone的根目录,然后转到Fw/Python,然后再转到Gds安装

一个坑点

这个坑我现在也不知道为什么,但硬是通过强制方式使其编译成功了
原文如下:

Testing F Autocoder Installation Via Building Ref Application:
cd Ref 
fprime-util generate 
fprime-util build --jobs "$(nproc ||printf '%s\n' 1)" 

就是通过cmake生成可执行文件测试是否安装成功,就这三行代码,我硬是装了半天

  • 首先第一个问题:fprime-util generate失败
    这个问题的具体输出我忘记了,应该是缺某些包,好像是flask,解决措施如下:
rm -rf build-fprime-xxxxx
conda install Flask
fprime-util generate

首先要删除cmake产生的build文件夹,否则会提示你clean-up,然后安装必要包
但使用pip install的话有些包的依赖没法安装,会报错,所以用conda直接安装
最后generate即可

  • 第二个问题:encode报错
    这个问题就比较迷了,因为python3默认的编码方式是utf-8,理论上不应该出现这个问题,但它还是出现了,错误描述如下:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1156-1157: ordinal not in rang

这里我按照网上教程去设置python的默认编码方式仍然是这个错误,也设置了环境变量为utf-8,但还是一样,因此不应该是这个的问题。
我的语言设置如下

(fprime) lin@lin-SVP1122A1J:~/桌面/FPrime/Ref$ locale
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:zh
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=en_US.utf8

所以我一行行找问题,问题最后定位在Cheetah3这个包的某个文件,文件名为:

envs/fprime/lib/python3.7/site-packages/Cheetah3-3.2.6.post1-py3.7-linux-x86_64.egg/Cheetah/Template.py

最后直接修改了Cheetah3的源文件,强制设置utf-8编码,大概在779行左右,源码如下:

		outputEncoding = 'ascii'
        compiler = None
        if useCache and cacheHash and cacheHash in klass._CHEETAH_compileCache:
            cacheItem = klass._CHEETAH_compileCache[cacheHash]
            generatedModuleCode = cacheItem.code
        else:
            compiler = compilerClass(source, file,
                                     moduleName=moduleName,
                                     mainClassName=className,
                                     baseclassName=baseclassName,
                                     mainMethodName=mainMethodName,
                                     settings=(compilerSettings or {}))
            if commandlineopts:
                compiler.setShBang(commandlineopts.shbang)
            compiler.compile()
            generatedModuleCode = compiler.getModuleCode()
            outputEncoding = compiler.getModuleEncoding()
            #--------------------------加入强制设置编码方式-----------------#
            outputEncoding = 'utf8'
            #-------------------------end of update---------------------------#
        if not returnAClass:
            # This is a bit of a hackish solution to make sure
            # we're setting the proper encoding on generated code
            # that is destined to be written to a file.
            if not outputEncoding == 'ascii':
                generatedModuleCode = generatedModuleCode.split('\n')
                generatedModuleCode.insert(
                    1, '# -*- coding: %s -*-' % outputEncoding)
                generatedModuleCode = '\n'.join(generatedModuleCode)

            return generatedModuleCode.encode(outputEncoding)

估计问题就是在cheetah3调用时设置的编码方式默认为ascii导致的问题,所以我直接强制设置了,设置完成后build就很顺利到100%

  1. 第三个问题:flask报错
    nmd,又是flask,这个是在执行最后一个命令发生的,命令为:
fprime-gds -g html -r build-artifacts

错误为:

ImportError: cannot import name 'secure_filename' from 'werkzeug' (/home/lin/anaconda3/envs/fprime/lib/python3.7/site-packages/werkzeug/__init__.py)

这个是可以搜到解决方案的,解决方案如下:
https://blog.csdn.net/sinat_28521487/article/details/105727870
如果找不到flask_uploads.py,可以在虚拟环境的dist-package下键入:

find . -name "flask*.py"

然后编辑即可
最后的效果如图:
在这里插入图片描述

总结

md,一行命令一个bug,我现在都不知道是不是因为是我的环境太奇葩还是nasa这帮人压根当这个是beta版,麻了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个使用PyCharm编写的深度学习预测波士顿房价的代码,其中使用了神经网络模型和相关的科学计算库、数据交换格式包和绘图工具包: ``` # 导入相关库 import numpy as np import json import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 定义神经网络类 class Network(object): def __init__(self, sizes): self.num_layers = len(sizes) self.sizes = sizes self.biases = [np.zeros((y, 1)) for y in sizes[1:]] self.weights = [np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:])] def feedforward(self, a): for b, w in zip(self.biases, self.weights): a = sigmoid(np.dot(w, a) + b) return a def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None): if test_data: n_test = len(test_data) n = len(training_data) for j in range(epochs): np.random.shuffle(training_data) mini_batches = [training_data[k:k+mini_batch_size] for k in range(0, n, mini_batch_size)] for mini_batch in mini_batches: self.update_mini_batch(mini_batch, eta) if test_data: print("Epoch {0}: {1} / {2}".format(j, self.evaluate(test_data), n_test)) else: print("Epoch {0} complete".format(j)) def update_mini_batch(self, mini_batch, eta): nabla_b = [np.zeros(b.shape) for b in self.biases] nabla_w = [np.zeros(w.shape) for w in self.weights] for x, y in mini_batch: delta_nabla_b, delta_nabla_w = self.backprop(x, y) nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)] nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)] self.weights = [w-(eta/len(mini_batch))*nw for w, nw in zip(self.weights, nabla_w)] self.biases = [b-(eta/len(mini_batch))*nb for b, nb in zip(self.biases, nabla_b)] def backprop(self, x, y): nabla_b = [np.zeros(b.shape) for b in self.biases] nabla_w = [np.zeros(w.shape) for w in self.weights] activation = x activations = [x] zs = [] for b, w in zip(self.biases, self.weights): z = np.dot(w, activation)+b zs.append(z) activation = sigmoid(z) activations.append(activation) delta = self.cost_derivative(activations[-1], y) * sigmoid_prime(zs[-1]) nabla_b[-1] = delta nabla_w[-1] = np.dot(delta, activations[-2].transpose()) for l in range(2, self.num_layers): z = zs[-l] sp = sigmoid_prime(z) delta = np.dot(self.weights[-l+1].transpose(), delta) * sp nabla_b[-l] = delta nabla_w[-l] = np.dot(delta, activations[-l-1].transpose()) return (nabla_b, nabla_w) def evaluate(self, test_data): test_results = [(np.argmax(self.feedforward(x)), y) for (x, y) in test_data] return sum(int(x == y) for (x, y) in test_results) def cost_derivative(self, output_activations, y): return (output_activations-y) # 定义sigmoid函数和其导数 def sigmoid(z): return 1.0/(1.0+np.exp(-z)) def sigmoid_prime(z): return sigmoid(z)*(1-sigmoid(z)) # 加载数据集 with open('housing.data', 'r') as f: data = f.readlines() data = [list(map(float, x.strip().split())) for x in data] data = [(np.array(x[:-1]).reshape(13, 1), x[-1]) for x in data] # 划分训练集和测试集 training_data, test_data = data[:-10], data[-10:] # 初始化神经网络 net = Network([13, 5, 1]) # 训练神经网络 net.SGD(training_data, 1000, 10, 0.5, test_data=test_data) # 绘制预测结果 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = [x[0][0] for x in test_data] y = [x[0][1] for x in test_data] z = [x[1] for x in test_data] ax.scatter(x, y, z, c='r', marker='o') x = np.arange(0, 1, 0.1) y = np.arange(0, 1, 0.1) x, y = np.meshgrid(x, y) z = np.array([net.feedforward(np.array([xi, yi]).reshape(2, 1))[0][0] for xi, yi in zip(x, y)]) ax.plot_surface(x, y, z) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值