左神基础班-猫狗队列

博客围绕左神基础班的猫狗队列展开,虽未给出具体内容,但推测会涉及猫狗队列相关的信息技术知识,如数据结构、算法等方面的讲解。
#include<queue>
#include<iostream>
using namespace std;

class Pet{
private:
	int type;
public:
	Pet(){

	}
	Pet(int type){
		this->type = type;
	}
	int getType(){
		return type;
	}
	void setType(int i){
		type =i;
	}
};
class Cat:public Pet
{
};
class Dog:public Pet{
};
class Elem{
private:
	Pet p;
	int num;
public:
	Elem(){}
	Elem(Pet p, int num){
		this->p = p;
		this->num = num;
	}
	Pet getPet(){
		return p;
	}
	void setPet(Pet p){
		this->p = p;
	}
	void setNum(int num){
		this->num = num;
	}
	int getNum(){
		return num;
	}
};
class CatDogQueue{
private:
	queue<Elem> cat;
	queue<Elem> dog;
public:
	void push(Elem elem){
		if(elem.getPet().getType() == 1){
			cat.push(elem);
		}else{
			dog.push(elem);
		}
	}
	Pet pollAll(){
		Pet pet;
		if(!cat.empty() && !dog.empty()){
			if(cat.front().getNum() < dog.front().getNum()){
				pet = cat.front().getPet();
				cat.pop();
			}else{
				pet = dog.front().getPet();
				dog.pop();
			}
		}else if(!cat.empty()){
			pet = cat.front().getPet();
			cat.pop();
		}else if(!dog.empty()){
			pet = dog.front().getPet();
			dog.pop();
		}
		return pet;
	}
	Pet pollCat(){
		if(!cat.empty()){
			return cat.front().getPet();
		}

	}
	bool isEmpty(){
		if(cat.empty() && dog.empty()){
			return true;
		}else{
			return false;
		}
	}
	bool catIsEmpty(){
		if(cat.empty())
			return true;
		return false;
	}
	bool dogIsEmpty(){
		if(dog.empty())
			return true;
		return false;
	}
};
int main(){
	CatDogQueue cdq;
	
	Cat c;
	c.setType(1);
	Elem e1;
	e1.setPet(c);
	e1.setNum(1);

	Dog d;
	d.setType(2);
	Elem e2;
	e2.setPet(d);
	e2.setNum(2);

	cdq.push(e1);
	cdq.push(e2);
	Pet p = cdq.pollAll();
	cout << "pollall is :" << p.getType()<<endl;
	p = cdq.pollAll();
	cout << "pollall is :" << p.getType()<<endl;
	if(cdq.isEmpty())
		cout << "now,queue is empty "<<endl;
	if(cdq.catIsEmpty())
		cout << "now,cat queue is empty "<<endl;
	if(cdq.dogIsEmpty())
		cout << "now,dog queue is empty "<<endl;
	
	Cat c2;
	c2.setType(1);
	Elem e3;
	e3.setPet(c2);
	e3.setNum(3);
	cdq.push(e3);
	cout << "cat enqueue " << endl;
	if(!cdq.catIsEmpty()){
		cout << "当猫队列有猫时,直接返回猫:" <<cdq.pollCat().getType() << endl;
	}
	return 0;
}

 

### 使用卷积神经网络实现猫狗图片分类 #### 构建模型架构 为了构建一个有效的猫狗二元分类器,可以采用经典的卷积神经网络(CNN)结构。CNN特别适合处理图像数据,因为其能够自动提取特征并减少人工设计特征的工作量。 ```python from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout model = Sequential([ Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)), MaxPooling2D(pool_size=(2, 2)), Conv2D(64, (3, 3), activation='relu'), MaxPooling2D(pool_size=(2, 2)), Conv2D(128, (3, 3), activation='relu'), MaxPooling2D(pool_size=(2, 2)), Flatten(), Dense(512, activation='relu'), Dropout(0.5), Dense(1, activation='sigmoid') ]) ``` 此代码定义了一个简单的CNN模型,其中包含了多个卷积层和池化层用于学习空间层次上的局部模式,并通过全连接层完成最终的分类任务[^1]。 #### 加载与预处理数据集 对于输入到模型中的每张图片都需要做标准化处理,使得不同尺寸或颜色分布的数据能被统一表示: ```python import numpy as np from tensorflow.keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1./255) training_set = train_datagen.flow_from_directory('dataset/training_set', target_size=(150, 150), batch_size=32, class_mode='binary') test_set = test_datagen.flow_from_directory('dataset/test_set', target_size=(150, 150), batch_size=32, class_mode='binary') ``` 这段脚本展示了如何利用`ImageDataGenerator`类来增强训练样本多样性以及调整测试集中图像大小至固定规格以便于喂入网络中进行计算[^2]. #### 编译及训练模型 编译阶段指定了损失函数、优化算法以及其他评价指标;而训练过程则是让机器不断迭代更新权重直至达到满意的性能水平: ```python model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) history = model.fit(training_set, steps_per_epoch=len(training_set), epochs=25, validation_data=test_set, validation_steps=len(test_set)) ``` 上述命令设置了Adam作为梯度下降方法之一,并选择了交叉熵作为衡量误差的标准,在此基础上执行了为期二十五轮次的学习循环以期获得更好的泛化能力. #### 模型评估与保存 当完成了全部周期之后就可以对新收集来的未知类别对象做出预测判断了。同时也可以把已经训练好的参数序列化存盘下来方便以后重复调用而不必每次都重新开始漫长的拟合流程. ```python # Save the entire model to a HDF5 file. model.save('cat_dog_classifier.h5') # Later you can recreate the exact same model purely from the file: #from tensorflow.keras.models import load_model #new_model = load_model('cat_dog_classifier.h5') ``` 这样就实现了完整的从准备环境到最后部署上线的一套工作流.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值